Renklauf
Renklauf

Reputation: 981

Match members of a tuple to members of a list, python

I thought this would be fairly trivial but it's turned out be more difficult than I'd anticipated.

I have a list of tuples:

  tuples = [('apples', 2, 'apple'), ('oranges', 3, 'orange')]

I have list of lines containing a possible member of the tuple in the list of tuples:

 list_of_lines = ['banana 1234', 'apple 5678', 'oranges 4321','apples 8765', 'orange 1111')]

For tuple[0] and tuple[2] in tuples, I need to match to the member in first position in list_of_lines and get that member's value (in second position). The key thing is output these results on one line. For the first tuple in tuples, for example, the desired output would be:

 'apples', 8765, 2, 'apple',5678

i.e. the original members of the tuple, now with values matched from list_of_lines.

If we try to do this with

  for tup in tuples:
    for line in list_of_lines:
        if tuple[0] == line.split()[0]:
            print tuple[0], line.split()[1]
        if tuple[2] == line.split()[0]:
            print tuple[2], line.split()[1]

then nothing is on one line and we get repeated matches.

Upvotes: 1

Views: 338

Answers (4)

Praveen Gollakota
Praveen Gollakota

Reputation: 39010

It is easier if you convert your list_of_lines to a dict first.

>>> tuples = [('apples', 2, 'apple'), ('oranges', 3, 'orange')]
>>> list_of_lines = ['banana 1234', 'apple 5678', 
...                  'oranges 4321','apples 8765']
>>> line_no = dict(x.split() for x in list_of_lines)
>>> result = [(x[0], line_no.get(x[0], None), x[1], 
               x[2], line_no.get(x[2], None)) for x in tuples]
>>> result
[('apples', '8765', 2, 'apple', '5678'), ('oranges', '4321', 3, 'orange', None)]

Upvotes: 2

NPE
NPE

Reputation: 500963

Rather than scanning list_of_lines every time you need to locate an entry, it's easier to pre-process the list into a dictionary:

tuples = [('apples', 2, 'apple'), ('oranges', 3, 'orange')]
list_of_lines = list_of_lines = ['banana 1234', 'apple 5678', 'oranges 4321','apples 8765', 'orange 1111']
d = dict(l.split() for l in list_of_lines)
for t in tuples:
    print "'%s', %s, %s, '%s', %s" % (t[0], d[t[0]], t[1], t[2], d[t[2]])

This prints out:

'apples', 8765, 2, 'apple', 5678
'oranges', 4321, 3, 'orange', 1111

Upvotes: 0

kevintodisco
kevintodisco

Reputation: 5191

I'll give this a shot:

for tup in tuples:
    matches = []
    for line in list_of_lines:    
        if tup[0] == line.split()[0]:
            matches.append([tup[0], str(line.split()[1]), str(tup[1])])
        if tup[2] == line.split()[0]:
            matches.append([tup[2], str(line.split()[1])])
    if len(matches) > 0:
        matches = [", ".join(x) for x in matches]
        print ", ".join(matches)

Output is:

apple, 5678, apples, 8765, 2
oranges, 4321, 3

Upvotes: 0

Zac B
Zac B

Reputation: 4242

Try an existence check on the variables and an after-the-fact print statement to ensure that they're on the same line.

tuples = [('apples', 2, 'apple'), ('oranges', 3, 'orange')]
list_of_lines = ['banana 1234', 'apple 5678', 'oranges 4321','apples 8765']
for tup in tuples:
    first = False
    second = False
    for line in list_of_lines:
        if tup[0] == line.split(' ')[0]:
            first = tup[0] , line.split()[1]
        if tup[2] == line.split(' ')[0]:
            second = tup[2] , line.split()[1]
        if first and second:
            print first + second

Output is: ('apples', '8765', 'apple', '5678')

Not quite sure if I understood the question, but I hope this got you in the right direction.

Upvotes: 0

Related Questions