2012-08-08 37 views
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號,然後組織名單通過日期和時間,所以我誠實地只需要每個人或身份證號碼的第一個條目。我很抱歉,如果這是非常基本的,但我無法找到很多可以幫助。

+2

你有什麼數據類型? 8:00是一個字符串還是數字?你在這裏展示的片段不能是真實的代碼,因爲你沒有任何明顯的字符串(如名稱)的引號,這使得很難判斷其他項目。 – Blckknght 2012-08-08 11:48:47

+0

你的第一個代碼塊有一個「Jane Willams」和一個「Jane Williams」。那是故意的嗎? – Kevin 2012-08-08 11:55:58

回答

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'] 
]