Custom Search

Tuesday, September 21, 2010

Python How find common elements in two list

>>> a = [2,4,6]
>>> b = [7,8,2]

>>> p = [x for x in b for y in a if x==y]
>>> p
[2]

=============================

ZIP --- > to make tuple from lists

>>> a = [2,4,6]
>>> b = [7,8,2]

>>> zip(a,b)

[(2, 7), (4, 8), (6, 2)]

-----------------------

>>> a = [2,4,6]
>>> b = [7,8,2]
>>> c = [4,5,6]

>>> zip(a,b,c)

[(2, 7, 4), (4, 8, 5), (6, 2, 6)]

No comments:

Post a Comment