Comparing two lists in Python

后端 未结 4 602
旧时难觅i
旧时难觅i 2020-12-30 10:41

So to give a rough example without any code written for it yet, I\'m curious on how I would be able to figure out what both lists have in common.

Example:

         


        
4条回答
  •  猫巷女王i
    2020-12-30 11:07

    i think this is what u want ask me anything about it i will try to answer it

        listA = ['a', 'b', 'c']
    listB = ['a', 'h', 'c']
    new1=[]
    for a in listA:
        if a in listB:
            new1.append(a)
    
    john = 'I love yellow and green'
    mary = 'I love yellow and red'
    j=john.split()
    m=mary.split()
    new2 = []
    for a in j:
        if a in m:
            new2.append(a)
    x = " ".join(new2)
    print(x)
    

提交回复
热议问题