How can I compare two lists in python and return matches

后端 未结 19 2337
误落风尘
误落风尘 2020-11-22 04:16

I want to take two lists and find the values that appear in both.

a = [1, 2, 3, 4, 5]
b = [9, 8, 7, 6, 5]

returnMatches(a, b)

would return

19条回答
  •  悲哀的现实
    2020-11-22 05:06

    >>> s = ['a','b','c']   
    >>> f = ['a','b','d','c']  
    >>> ss= set(s)  
    >>> fs =set(f)  
    >>> print ss.intersection(fs)   
       **set(['a', 'c', 'b'])**  
    >>> print ss.union(fs)        
       **set(['a', 'c', 'b', 'd'])**  
    >>> print ss.union(fs)  - ss.intersection(fs)   
       **set(['d'])**
    

提交回复
热议问题