python中怎么比较两个列表的大小_Python:找到两个列表中存在的给定长度的公共子列表...
我必須找到一個有效的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
總結
以上是生活随笔為你收集整理的python中怎么比较两个列表的大小_Python:找到两个列表中存在的给定长度的公共子列表...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab fftshift_matl
- 下一篇: python写windows程序_【Py