common elements between list of lists and lists

Discussion in 'Python' started by antar2, Jul 17, 2008.

  1. antar2

    antar2 Guest

    Hello,

    I am a beginner in python.
    following program prints the second element in list of lists 4 for the
    first elements in list 4 that are common with the elements in list 5


    list4 = [['1', 'a'],['4', 'd'],['8', 'g']]
    list5 = ['1', '2', '3']

    for j in list4:
    for k in list5:
    if j[0] == k:
    print j[1]

    Result: a

    I would like to do the same thing starting with following lists, where
    the numbers in list 5 are without ''. Is there a way to convert
    integers in a list to integers in '' ? This is based on a situation
    where I want to find common numbers between a list and a list of lists
    where the numbers in the list are without '' and the numbers in the
    list of lists are with ''


    list4 = [['1', 'a'],['4', 'd'],['8', 'g']]
    list5 = [1, 2, 3]

    This might be a stupid question, but anyway, thanks for your answer
    It is not my first post on this site. In some way it is not possible
    to react on the messages that I receive to thank the persons that
    react. Anyway, thanks a lot
     
    antar2, Jul 17, 2008
    #1
    1. Advertising

  2. >>> list4 = [['1', 'a'],['4', 'd'],['8', 'g']]
    >>> list5 = [1, 2, 3]
    >>> set5 = set(list5)
    >>> [x for n, x in list4 if int(n) in set5]

    ['a']
     
    Alexandr N Zamaraev, Jul 17, 2008
    #2
    1. Advertising

  3. antar2

    Bighead Guest

    On Jul 17, 4:30 pm, antar2 <> wrote:
    > Hello,
    >
    > I am a beginner in python.
    > following program prints the second element in list of lists 4 for the
    > first elements in list 4 that are common with the elements in list 5
    >
    > list4 = [['1', 'a'],['4', 'd'],['8', 'g']]
    > list5 = ['1', '2', '3']
    >
    > for j in list4:
    > for k in list5:
    > if j[0] == k:
    > print j[1]
    >
    > Result: a
    >
    > I would like to do the same thing starting with following lists, where
    > the numbers in list 5 are without ''. Is there a way to convert
    > integers in a list to integers in '' ? This is based on a situation
    > where I want to find common numbers between a list and a list of lists
    > where the numbers in the list are without '' and the numbers in the
    > list of lists are with ''
    >
    > list4 = [['1', 'a'],['4', 'd'],['8', 'g']]
    > list5 = [1, 2, 3]
    >
    > This might be a stupid question, but anyway, thanks for your answer
    > It is not my first post on this site. In some way it is not possible
    > to react on the messages that I receive to thank the persons that
    > react. Anyway, thanks a lot


    By "integer without ''" you mean integers not embraced by single
    quotes, right?

    Actually, '1' is a string, not an integer. If you want to normalize
    the first elements of all the lists in list4, just use int() to
    convert them.

    That is:

    list4 = [['1', 'a'],['4', 'd'],['8', 'g']]
    list5 = ['1', '2', '3']

    set5 = set(map(int, list5))
    list4 = [[int(i[0]), i[1]] for i in list4]

    for j in list4:
    if j[0] in set5: print j[1]

    You can have a try :)
     
    Bighead, Jul 17, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-2?Q?Grzegorz_=A6lusarek?=

    how to match n- lists for a common elements.

    =?ISO-8859-2?Q?Grzegorz_=A6lusarek?=, Apr 13, 2006, in forum: Python
    Replies:
    1
    Views:
    403
    bruno at modulix
    Apr 13, 2006
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    433
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. ardief
    Replies:
    14
    Views:
    754
    Paddy
    Feb 3, 2007
  4. Adam Hartshorne
    Replies:
    2
    Views:
    387
    Nitin Motgi
    Jan 27, 2006
  5. mclovin
    Replies:
    20
    Views:
    993
    Raymond Hettinger
    Jul 8, 2009
Loading...

Share This Page