如何查找列表的名称

时间:2016-01-07 15:46:35

标签: python string list

我想找到列表的名称,我怎么能在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': 

1 个答案:

答案 0 :(得分: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
            ...