2014-01-25 12 views
0

我有一個問題,我一直想要問一下現在。我有一些代碼,我試圖檢查每個項目的引用列表朝向另一個列表,所有這些INLINE。例如:內聯循環Python。檢查列表中的每個成員到另一個列表內嵌

if int(word[:-1])>=1 and int(word[:-1])<=31 and for word in LIST1, word in LIST2: 
       day = word[:-1] 

更具體地,這是數據刮程序來篩選出的數據的各種領域,例如日期和月份的一部分。

months = ["January","February",etc......] 
days = ["monday,","tuesday,","wednesday,","thursday,","friday,","saturday,","sunday,"] 
if word.lower() in days and For i in line.split(), months.contains(i) 

因此,例如,在list1中,我每週都有一天。如果LIST2包含一週中的任何一天(不是全部),則這將評估爲真。有誰知道一個光滑的方式來做到這一點?謝謝。

+0

能否請您解釋與樣品的輸入和預期輸出你的問題? – thefourtheye

回答

3
if set(list2) & set(list1): 
    print "there is something in list2 that matches something in list1" 

print "The days in list2 are", set(list2) & set(list1) 

+0

好的,如此內聯,我可以將每個列表指定爲一個集合並使用&返回一個布爾值? –

+0

嘿,那就像一個魅力!謝謝! –

+0

@嚴格來說,在一個bash-shell中,&會返回一個集合。如果它具有成員,則該集合在布爾上下文中評估爲True。 – GreenAsJade

相關問題