我必须找到一个高效的python代码来完成以下工作:
找到包含在两个给定列表中的至少一个n
连续元素序列。
例如,对于n=3
,这两个列表的结果将为['Tom', 'Sam', 'Jill']
lst1 = ['John', 'Jim', 'Tom', 'Sam', 'Jill', 'Chris']
lst2 = ['Chris', 'John', 'Tom', 'Sam', 'Jill', 'Jim']
下面的示例代码做到了这一点,但如果我必须比较数十万行/列表,它将永远做同样的事情。任何关于如何优化此代码的性能以处理大量数据的建议都将不胜感激!
lst1 = ['John', 'Jim', 'Tom', 'Sam', 'Jill', 'Chris']
lst2 = ['Chris', 'John', 'Tom', 'Sam', 'Jill', 'Jim']
strNum = 3 #represents number of consecutive strings to search for
common_element_found = 'False'
common_elements = []
lst1length = len(lst1) - (strNum - 1)
lst2length = len(lst2) - (strNum - 1)
for x in range(lst1length):
ConsecutiveStringX = lst1[x] + ' ' + lst1[x + 1] + ' ' + lst1[x + 2]
for y in range(lst2length):
ConsecutiveStringY = lst2[y] + ' ' + lst2[y + 1] + ' ' + lst2[y + 2]
if ConsecutiveStringY == ConsecutiveStringX:
common_element_found = 'True'
common_elements.append(ConsecutiveStringY)
print('Match found: ' + str(common_elements))
break
if common_element_found == 'True':
common_element_found = 'False'
break
https://stackoverflow.com/questions/51389795
复制相似问题