給定一個包含噪音包圍的已知圖案的列表,是否有一種優雅的方式來獲得與該模式相同的所有項目。請參閱下面的我的粗略代碼。列表中的優雅發現子列表
list_with_noise = [7,2,1,2,3,4,2,1,2,3,4,9,9,1,2,3,4,7,4,3,1,2,3,5]
known_pattern = [1,2,3,4]
res = []
for i in list_with_noise:
for j in known_pattern:
if i == j:
res.append(i)
continue
print res
我們會得到2, 1, 2, 3, 4, 2, 1, 2, 3, 4, 1, 2, 3, 4, 4, 3
獎金:避免附加我如果全模式不存在(即,允許1,2,3,4而不是1,2,3)
例子:
find_sublists_in_list([7,2,1,2,3,4,2,1,2,3,4,9,9,1,2,3,4,7,4,3,1,2,3,5],[1,2,3,4])
[1,2,3,4],[1,2,3,4],[1,2,3,4]
find_sublists_in_list([7,2,1,2,3,2,1,2,3,6,9,9,1,2,3,4,7,4,3,1,2,6],[1,2,3,4])
[1,2,3],[1,2,3],[1,2,3]
名單包含一個名爲元組。
盛宴你的眼睛http://en.wikipedia.org/wiki/String_searching_algorithm – YXD 2012-04-11 13:30:09
你可能想給一些輸入的例子和相應的預期輸出。目前形式的問題尚不清楚。 – NPE 2012-04-11 13:33:09
這是一個問題或編程練習? – 2012-04-11 13:33:46