How to search for an item in a list of lists?

前端 未结 5 738
星月不相逢
星月不相逢 2021-01-25 00:15

How can I do something like this.

index=[[test1,test2,test3],[test4,test5,test6],[test7,test8,test9]]
if test5 is in index:
    print True
5条回答
  •  有刺的猬
    2021-01-25 00:55

    try

    index=[[test1,test2,test3],[test4,test5,test6],[test7,test8,test9]]
    flat_index=[item for sublist in index for item in sublist]
    if test5 is in flat_index:
        print True
    

    see also Making a flat list out of list of lists in Python

提交回复
热议问题