1
我在相關問題上閱讀了很多問題,但他們都沒有回答我的問題。我有兩個列表:在Python中比較兩個列表的項目
List A = ['nike', 'adidas', 'reebok']
List B = ['sneakers', 'sneaker shoes', 'adidas shoes', 'nike', 'any shoe', 'all nikes', 'a nike shoe']
現在,我想看看B中的某處存在表A的項目,使其返回:
List result: [False, False, True, True, False, True, True]
真正代表列表B中的實例,其中一個A的項目被匹配。到目前爲止,我已經使用了這個代碼,這看起來非常低效。
for j in range(len(lista)):
for k in b:
if j in k:
lista[j] = 'DELETE'
cuent = lista.count('DELETE')
for i in range(cuent):
lista.remove('DELETE')
在此先感謝和抱歉,如果確有一個答案 - 一個小時後,我已經失去了在計算器宇宙:)
編輯找到它的希望:對不起,不使我自己清楚 - 我不是在尋找完全匹配,我正在尋找詞組匹配。再次抱歉!
這是完美的 - 我還不熟悉這個很短的「(關鍵字中的關鍵字在關鍵字中的關鍵字)」 - 表達式 - 你知道我可以在哪裏瞭解更多?謝謝! – oliver13 2013-04-30 09:04:40
@ oliver13查看[文檔中的列表解析](http://docs.python.org/2/tutorial/datastructures.html#list-comprehensions) – TerryA 2013-04-30 09:06:22
@ oliver13:這被稱爲「生成器表達式」。見例如http://stackoverflow.com/q/1756096/989121解釋。 – georg 2013-04-30 09:07:34