hi,

my problem I can't solve:
each of my two lists consists of an ID and runID. The output I need are those elements who are not in list2 and the max Value of runID of each ID.
e.g.
list1 = [('1101', '2'), ('1101', '3'), ('1101', '4'), ('4472', '2'), ('4472', '3'), ('4472', '4'), ('4472', '5'), ('5419', '2')]
list2 = [('1101', '3'), ('4472', '5'), ('5419', '3')]

The correct output should be: ('1101','4')

for element in t5_list:
	if element not in zzt_list:
            # Check for max value ??

Has anybody an idea how I can solve my problem ??

You problem is not clear to me :
Why is ('1101','4') the correct output ?
'1101' is in the second list...
Why not ('4472', '4') or ('5419', '2')
Are these tuples displayed if and only if the run id of the id in list 1 is greater than any run id of the same id in list 2 (or if the id isn't in list 2)

I *think* i understand what you mean, but so i don't give you the whole answer i'll try and point you in the right direction. I think for something like this you should have a look at using sets
http://docs.python.org/library/sets.html
These are great for easily taking out any of the same value.
So

#Make our lists
>>> list1 = [('1101', '2'), ('1101', '3'), ('1101', '4'), ('4472', '2'), ('4472', '3'), ('4472', '4'), ('4472', '5'), ('5419', '2')]
>>> list2 = [('1101', '3'), ('4472', '5'), ('5419', '3')]
>>> set1 = set(list1)
>>> set2 = set(list2)
>>> set1-set2
set([('1101', '2'), ('4472', '4'), ('4472', '3'), ('4472', '2'), ('1101', '4'), ('5419', '2')])

So see by going set1-set2 we only got the values that were in set1 but not in set2, which seems to be what you want.

So with that in mind, have a go at getting the max value yourself and come back if you are having any issues, i always think you learn a lot more if you discover for yourself :)

good luck

Here's what I'd do. By converting the sorted list into a dictionary you'll end up with the highest value for each key in the list

list1 = [('1201', '3'), ('1101', '4'), ('1101', '2'), ('1101', '3'), ('4472', '3'), ('4472', '1'), ('4472', '2'), ('4472', '0'), ('5419', '2')]
list2 = [('1101', '3'), ('4472', '5'), ('5419', '3'), ('453', '3')]

store1 = dict(sorted(list1))
store2 = dict(sorted(list2))

for key, value in store1.items():
    if key not in store2:
        del store1[key]
    elif value < store2[key]:
        store1[key] = store2[key]
        
print store1
# output is: "{'4472': '5', '5419': '3', '1101': '4'}"

Here is how you can write it with itertools

import itertools as itt
from operator import itemgetter

list1 = [
    ('1101', '2'), ('1101', '3'), ('1101', '4'), ('4472', '2'), ('4472', '3'),
    ('4472', '4'), ('4472', '5'), ('5419', '2')
]
list2 = [
    ('1101', '3'), ('4472', '5'), ('5419', '3')
]

# A generates the items in list1 which are not in list2
A = itt.ifilterfalse(set(list2).__contains__, list1)
# B groups the items according to the ID. This works if list1
# is initially sorted. If not, we can put sorted(A) in the definition of B
B = itt.groupby(A, itemgetter(0))
# C computes the max runID of each group
C = (max(g, key=itemgetter(1)) for (k, g) in B)
print list(C)

# the same in a one liner (ugly)
# print list(max(g, key=itemgetter(1)) for (k, g) in itt.groupby(sorted(itt.ifilterfalse(set(list2).__contains__, list1)), itemgetter(0)))


""" my output -->
[('1101', '4'), ('4472', '4'), ('5419', '2')]
"""

:)

Edited 6 Years Ago by Gribouillis: n/a

Here's what I'd do. By converting the sorted list into a dictionary you'll end up with the highest value for each key in the list

Whoops, looking back on the original post this isn't correct. I was finding the overlapping items.

The basic approach is the same though: sort lists, convert to dictionaries, iterate through keys and values of first dictionary and compare to second's.

list1 = [('1201', '3'), ('1101', '4'), ('1101', '2'), ('1101', '3'), ('4472', '3'), ('4472', '1'), ('4472', '2'), ('4472', '0'), ('5419', '2')]
list2 = [('1101', '3'), ('4472', '5'), ('5419', '3'), ('453', '3')]

store1 = dict(sorted(list1))
store2 = dict(sorted(list2))

for key, value in store1.items():
    if (key in store2) and (store2[key] > value):
        del store1[key]
        
print store1.items()
# output is: [('1201', '3'), ('1101', '4')]

Thanx everybody for your help. I've to learn more about dictionary!!

This article has been dead for over six months. Start a new discussion instead.