paperstsoap paperstsoap - 6 months ago 21
Python Question

Check if one list matches a sublist inside of a list by index

I have a data base that consist of about 800 16 number sequences. The sequences are a unique combination of ones and zeros. There is a process that outputs 15 of the numbers in the sequence and I want to look through the data base (historical data) and find all the matches excluding the last number. I started using a list of sublist. I adapted some code I found in another question asked, however it doesn't fully work for my application. The code is:

T = [0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
joined_T_Search = int(''.join(map(str,T_vein)))
joined_T_Search = str(joined_T_Search)
match_T = [] #empty list to append closest match to
#Match search algorithm
for text in data:
if joined_T_Search in text:
match_T.append(text)
#grabs last digit to indicate hit/miss
filter_output_T = match_T[0][15] #grabs last digit within sublist
print match_T
print len(match_T) #indicates if multiple results show up


It works well most of the time, however it sometimes outputs something like this:

[0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0] #input
['0001000010000000', '1000010000000100', '0000100001000000', '0101000010000000', '0110100001000000', '0100001000000000', '1000010000000010', '1000010000000001', '1000100001000000', '1000010000001010', '0000100001000000', '0001000010000000', '0001000010000000', '0101100001000000', '1110000100000000', '0100100001000000', '0110000100000000', '1000010000000000'] #output


What I want (which it sometimes does if there are multiple similar combinations) is this:

[1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0] #input
['1000001000100000', '1000001000100001', '1000001000100001', '1000001000100001'] #output


Ultimately, this last digit will be used to make a decision, so i will need to know its index. In the case of the first 15 matching and say 3 zeros and 2 ones appearing as the 16th number, I want to pick the most frequent number. This number could be appended to a list or similar method. It is important that the order of the numbers stay the same.

I think that a numpy 2D array is a better option then a list of list for a multitude of reasons, however I am not sure how to implement this.

I have only been coding in python for a few months, so I am just starting to get the hang of it. I have a mechanical engineering degree so I am familiar with programming but am by no means a programmer. Thanks for the help!

Answer

If I understand your problem correctly, I would do the following:

T = [0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
joined_T_Search = ''.join(map(str,T))
match_T = []    #empty list to append closest match to

#Match search algorithm
for text in data:
    if joined_T_Search == text[:-1]:
        match_T.append(text[-1])
print match_T

if len(match_T) == 0:
    # Do something (nothing matched the input)
else:
    # Find the max of the last element
    print max(set(match_T), key=match_T.count)

This assumes that you only really care about the final digit that occurs most often. It's essentially what you were doing, but I only save the last digit and then find the max count of that set.

Comments