簡體   English   中英

將列表與列表列表進行比較

[英]Comparing lists with lists of lists

如何檢查 list2 的字符串是否在 list1 列表的字符串中?

例如:

list1 = [["CATTAC"], ["AGGA"]]
list2 = ["AT", "GG"]

如果 list2 的元素存在於 list1 中,則函數返回 true

    def my_find(list1, list2):
        for cur in list2:
            for cur_list in list1:
                if cur in cur_list:
                    return True
        return False

使用循環的簡單解決方案

list1 = [["CATTAC"], ["AGGA"]]
list2 = ["AT", "GG"]
for x in range(len(list1)):
    for y in range(len(list2)):
        if str(list1[x]).find(str(list2[y])) == -1 :
            print("NO")
        else :
            print("YES")

干得好:

list1 = [["CATTAC"], ["AGGA"]]
list2 = ["AT", "GG"]
res = [[l in e[0] for l in list2] for e in list1]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM