简体   繁体   中英

Function that returns a list of 6-element or 3-element tuples

I want to write a function that compare the 3 elements of each 3-tuple of two list with different sizes. Specifically, I want to write a function match as follows:

list1 = ['a','b', 'a', 'a', 'c', 'a', 'c', 'c', 'a']; #just an example of a list1
list2 = ['c','a', 'a', 'c', 'a', 'a']; #just an example of a list2

def match(list1, list2, storage = []):
    for i in range(0, len(list1), 3):
       for j in range(0, len(list2), 3):
       #matching rule
          if (list1[i] == list2[j+1] and list1[i] == list2[j+2]) or (list1[i] == list2[j+1] and (list1[i] == list1[i+1] or list1[i] == list1[i+2])) or (list1[i] == list1[i+2] or (list1[i] == list1[i+1]) and (list2[j] == list2[j+1] or list2[j] == list2[j+2])) or (list2[j] == list1[i+1] and list2[j] == list1[i+2]):
             storage.append([list1[i], list1[i+1], list1[i+2], list2[j], list2[j+1], list2[j+2]])
         else: storage.append([list1[i], list1[i+1], list1[i+2]])
    return storage; #outside of the outer 'for' loop - obtain a list of all 6-element and/or 3-element tuples               

Now after getting all the pairs of 3-tuples (eg 1 element of the list storage is of the form ['a','b', 'a','c','a', 'a'] ), I want to get the indices of all elements in storage that contains ['a', 'b', 'a'] as a portion of a 6-element list (eg ['a','b', 'a','c','a', 'a'] ) or 3-element list (eg ['a','b', 'a'] ) - the only 2 types of list in storage .

Question. Can anyone please help me with verifying if my function is correct, and then the part on finding indices? I would greatly appreciate any inputs.

This may helps you:

str1 = "abaacbabaacbabca" #...
l = []
l.append("caacaa")
l.append("ghijhg") # any 6 letter long string
l.append("acbaca")
for str2 in l:
    if any(str1[i:i+3] in str2 for i in range(len(str1)-3)):
        print(str2)

it returns (print):

caacaa
acbaca

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM