簡體   English   中英

如何檢查列表中的元素是否存在於另一個列表中

[英]How to check if an element in the list exists in another list

如何檢查列表中的元素是否存在於另一個列表中,以及是否確實將其追加到另一個列表中,如何使它獲取列表中的所有值?

common=[]

def findCommon(interActor,interActor1):
    for a in interActor:
        if a in interActor1:
            common.append(a)
    return common
interActor=['Rishi Kapoor','kalkidan','Aishwarya']
interActor1=['Aishwarya','Suman Ranganathan','Rishi Kapoor']

您可以使用列表推導來實現:

common = [x for x in iter_actor1 if x in iter_actor2]

或使用集合:

common = set(iter_actor1).intersection(iter_actor2)
interActor=['Rishi Kapoor','kalkidan','Aishwarya']
interActor1=['Aishwarya','Suman Ranganathan','Rishi Kapoor']
anotherlist = []

for x in interActor:
    if x in interActor1:
        anotherlist.append(x)

暫無
暫無

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

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