lundi 25 juillet 2016

List of Sub-Strings Search Returns Multiple Conditions At Once

I have a large list of sub-strings that I want to search through and find if two particular sub-strings can be found in a row. The logic is intended to look for the first sequence, then if it is found, look at the second sub-string and return all the matches (based off the first 15 characters of the 16 character sequence). If the first sequence can not be found, it just looks for the second sequence only, and finally, if that can not be found, defaults to zero. The matches are then appended to a list, which is processed further. The current code used is as follows:

dataA = ['0100101010001000',
'1001010100010001',
'0010101000100010',
'0101010001000110',
'1010100010001110',
'0101000100011100',
'1010001000111010',
'0100010001110100',
'1000100011101000',
'0001000111010000']
A_vein_1 = [0,1,0,0,1,0,1,0,1,0,0,0,1,0,0,0]
joined_A_Search_1 = ''.join(map(str,A_vein_1))
print 'search 1', joined_A_Search_1
A_vein_2 = [1,0,0,1,0,1,0,1,0,0,0,1,0,0,0]
joined_A_Search_2 = ''.join(map(str,A_vein_2))
match_A = []    #empty list to append closest match to
#Match search algorithm
for i,text in enumerate(data):
    if joined_A_Search_1 == text:
       if joined_A_Search_2 == data[i+1][:-1]:
            print 'logic stream 1'
            match_A.append(data[i+1][-1])
    if joined_A_Search_1 != text:
        if joined_A_Search_2 == text[:-1]:
            print 'logic stream 2'
            #print 'match', text[:-1]
            match_A.append(text[-1])
print ' A matches', match_A
try:
    filter_A = max(set(match_A), key=match_A.count)
except:
    filter_A = 0
    print 'no match A'
filter_A = int(filter_A)
print '0utput', filter_A

The issue is that I get a return of both logic stream 1 and logic stream 2, when I actually want it to be a strict one or the other, in this case only logic stream 1. An example of the output looks like this:

search 1 0100101010001000
search 2 100101010001000
logic stream 1
logic stream 2
logic stream 1
logic stream 2
logic stream 2

(Note: The list has been shortened, and the data inputs have been substituted in directly, as well as the print outs for the purposes of this post and error tracking)

Aucun commentaire:

Enregistrer un commentaire