0

我有一个列表列表。每个嵌套列表包含 4 或 5 个元素(ID、日期、时间、名称、注释)。我希望能够拉出包含每个人每天第一次的嵌套列表。目前我有:

NestedList = [[100, 08/08/2012, 8:00, John Smith], [100, 08/09/2012, 9:20, John Smith], [100, 08/08/2012, 10:00, John Smith], ..., [131, 08/10/2012, 8:00, Jane Williams], [131, 08/12/2012, 22:00, Jane Willams], ... (thousands of entries with hundreds of people)]

我想要这样的东西:

NewList = [[100, 8/08/2012, 8:00, John Smith], [100, 8/09/2012, 8:02, John Smith], ...,      [131, 8/08/2012, 8:00, Jane Williams], [131, 08/09/2012, 8:05, Jane Williams], ...]

时钟设置为 24 小时制而不是 12 小时制。我已经按 ID 号然后按日期和时间组织了列表,所以老实说,我只需要每个人或 ID 号的第一个条目。如果这是非常基本的,我深表歉意,但我找不到太多可以提供帮助的东西。

4

1 回答 1

1

听起来您想为每个日期名称对获取一个子列表。这似乎是字典的一个很好的用例:(日期,名称)是键,该对的最早记录是值。

#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']
]
于 2012-08-08T12:08:26.560 回答