我有一個看起來像這樣的數據:的Python for循環和數據
的列有姓名,ID,機號,日期
('Anthony', '1', '10', '4/3/2017')
('Anthony', '1', '11', '5/2/2017')
('Anthony', '1', '13', '12/30/2017
('Anthony', '1', '15', '8/20/2017'
('Anthony', '4', '17', '2/3/2018')
('Anthony', '4', '18', '3/28/2017'
('Bob', '1', '111', '4/3/2017')
('Bob', '1', '200', '5/2/2017')
('Bob', '1', '113', '12/30/2017')
('Bob', '1', '115', '8/20/2017')
('Bob', '4', '117', '2/3/2018')
('Bob', '4', '118', '3/28/2017')
我試圖找到獨特的名和ID的,然後比較任何日期並只返回將來最遠的日期。
理想我想輸出,看起來像:
('Anthony', '1', '12/30/2017')
('Anthony', '4', '2/3/2018')
('Bob', '1', '12/30/2017')
('Bob', '4', '2/3/2018')
我掙扎,因爲我有多個按鍵,我無法弄清楚如何使它發揮作用。有任何想法嗎?
編輯:這只是一個樣本,我有30個人的姓名和10個唯一的ID。所以我正在尋找一個For循環來解決這個問題。
氏如果列按「名稱」和「Id」排序,則只能使用 –
您可以使用['''operator.itemgetter(0,1)'''](https://docs.python.org/3/ library/operator.html#operator.itemgetter)作爲對數據進行排序和分組的關鍵。 – wwii