selecting the earliest entry in a list
If I have a list with people's names and dates, and I want only to keep the entry for the earliest date per person how do I do that? I want the final list to be alphabetical by last name, then first name and only contain the entry with the earliest date at the end.
Here is an example of the list and what I tried, but it just gave me back the same list again.
L1=['Smith, John, 1994', 'Smith, John, 1996', 'Smith, John, 1998', 'Smith, Joan, 1993', 'Smith, Joan, 1995', 'Smith, Jack, 1989', 'Smith, Jack, 1991', 'Jones, Adam, 2000', 'Jones, Adam, 1998', 'Jones, Sarah, 2002', 'Jones, Sarah, 2005', 'Brady, Tom, 2001', 'Brady, Tonya, 2002']
L1.sort()
L2= []
for item in L1:
if item.split(',')[:2] not in L2:
L2.append(item)
The final product should look like:
L2=['Brady, Tom, 2001', '开发者_StackOverflowBrady, Tonya, 2002', 'Jones, Adam, 1998', 'Jones, Sarah, 2002', 'Smith, Jack, 1989', 'Smith, Joan, 1993', 'Smith, John, 1994']
Any help or insight would be greatly appreciated!
Try
L1.sort()
[next(j) for i, j in itertools.groupby(L1, lambda x: x.rsplit(",", 1)[0])]
Your code does not work since you are searching L2
for item.split(',')[:2]
, which is only the name. But the strings in the list consist of the name and the year -- that's why the not in
always yields True
.
>>> from itertools import groupby
>>> [next(j) for i, j in groupby(sorted(L1), lambda x: x.rpartition(",")[0])] == L2
True
Python's built in list and tuple sorts are recursive. If your data was stored like so:
L1=[(1,2,3),
(2,3,4),
(1,1,3)]
It would sort your list by the first item in each tuple, then sort each group by the second item, and so on. The result being:
[(1, 1, 3), (1, 2, 3), (2, 3, 4)]
So if you break your strings into tuples of (Date, Last, First)
and then sort you will get the order you desire, but then you'll have to stitch your string back together.
精彩评论