2016-01-07 32 views
0

我想找到一個列表的名稱,我怎麼能在python中做到這一點?如何找到列表的名稱

這是我的代碼:

ab = "the sky is blue" #in my code i don't know what text is in ab 

#others lists... 
bannana = ["white", "red", "blue"] 
tomato = ["red", "shiny", "grey"] 
peach = ["séché", "mure", "moisi"] 
spicybannana = ['uigyig','iuig','iug'] 
#other lists... 

l1 = [bannana, tomato, peach] 

randomlist = randrange(0, len(l1)) 

for i in l1[randomlist]: 
    if i in ab: 
     #if list name contain 'bannana': 
+3

商店列表和該列表是價值,問題解決 –

+0

'if l1 [randomlist] == bannana:'比較一下,看看它們是否是相同的列表,是你在做什麼? – Jkdc

+0

@Jkdc不,因爲我想知道當前列表名稱是否包含'bannana',因爲我有其他名單bannana ...我想匹配它們全部 – mathieu

回答

2

按照@Tim_Castelijns建議 - 只要名單存放在詞典:與列表名稱的字典是關鍵

other_lists = { 
    'bannana': ["white", "red", "blue"], 
    'tomato': ["red", "shiny", "grey"], 
    'peach': ["séché", "mure", "moisi"] 
} 

randomlist = random.choice(['bannana', 'tomato', 'peach']) 
for i in other_lists[randomlist]: 
    if i in ab: 
     if 'bannana' == randomlist: # use 'in' for substring 
      ... 
+0

好的,謝謝我一直在尋找random.choice – mathieu