听起来您想为每个日期名称对获取一个子列表。这似乎是字典的一个很好的用例:(日期,名称)是键,该对的最早记录是值。
#uses an iterable `seq` to populate a dictionary.
#the function `keyFunc` will be called on each element of seq to generate keys.
#if two elements `a` and `b` have the same key,
#`compFunc(a,b)` will return which element should belong in the dict.
def make_dict(seq, keyFunc, compFunc):
d = {}
for element in seq:
key = keyFunc(element)
if key not in d:
d[key] = element
else:
d[key] = compFunc(d[key], element)
return d
#I've put all your elements in quotes so that it's valid python.
#You can use whatever types you prefer,
#as long as the date and name can be used as a key,
#and the time supports comparison.
NestedList = [
['100', '08/08/2012', '08:00', 'John Smith'],
['100', '08/09/2012', '09:20', 'John Smith'],
['100', '08/08/2012', '10:00', 'John Smith'],
['131', '08/10/2012', '08:00', 'Jane Williams'],
['131', '08/12/2012', '22:00', 'Jane Williams']
]
#the key is generated from the element's date and name
keyFunc = lambda x: (x[1], x[3])
#prefer the element with the smaller time
compFunc = lambda a,b: a if a[2] < b[2] else b
NewList = make_dict(NestedList, keyFunc, compFunc).values()
NewList.sort() #optional
print NewList
输出:
[
['100', '08/08/2012', '08:00', 'John Smith'],
['100', '08/09/2012', '09:20', 'John Smith'],
['131', '08/10/2012', '08:00', 'Jane Williams'],
['131', '08/12/2012', '22:00', 'Jane Williams']
]