對於熟悉Python列表的人員,我有一個愚蠢的問題。 我想獲得兩個列表中的常見項目。假設我有這樣的名單:兩個列表之間的重複/共同元素
dates_list = ['2016-07-08 02:00:02',
'2016-07-08 02:00:17',
'2016-07-08 02:00:03',
'2016-07-08 02:00:20',
'2016-07-08 02:01:08',
'2016-07-08 02:00:09',
'2016-07-08 02:01:22',
'2016-07-08 02:01:33']
,並命名爲「time_by_seconds」其中包含的一天的所有秒的名單列表:
time_by_seconds = [['2016-07-08 02:00:00',
'2016-07-08 02:00:01',
'2016-07-08 02:00:02',
'2016-07-08 02:00:03',
'2016-07-08 02:00:04',
'2016-07-08 02:00:05',
'2016-07-08 02:00:06',
etc ],
['2016-07-08 02:01:00',
'2016-07-08 02:01:01',
'2016-07-08 02:01:02',
'2016-07-08 02:01:03',
'2016-07-08 02:01:04',
etc ]]
這是我的代碼打印的項目,如果他們在此列表:
for item in dates_list:
for one_list in time_by_seconds:
if item in one_list:
print item
這是結果:
2016-07-08 02:00:02
2016-07-08 02:00:17
2016-07-08 02:00:03
2016-07-08 02:00:20
2016-07-08 02:01:08
2016-07-08 02:00:09
2016-07-08 02:01:22
2016-07-08 02:01:33
但是,如果我使用另一個列表,長度爲49,我有重複。具體而言,我必須有49個元素作爲結果,因爲所有這些日期都存在於我的time_by_seconds中。 這是列表:
beginning_time_list = ['2016-07-08 02:17:42',
'2016-07-08 02:05:35',
'2016-07-08 02:03:22',
'2016-07-08 02:26:33',
'2016-07-08 02:14:54',
'2016-07-08 02:05:13',
'2016-07-08 02:15:30',
'2016-07-08 02:01:53',
'2016-07-08 02:02:31',
'2016-07-08 02:00:08',
'2016-07-08 02:04:16',
'2016-07-08 02:08:44',
'2016-07-08 02:11:17',
'2016-07-08 02:01:40',
'2016-07-08 02:04:23',
'2016-07-08 02:01:34',
'2016-07-08 02:24:31',
'2016-07-08 02:00:27',
'2016-07-08 02:14:35',
'2016-07-08 02:00:57',
'2016-07-08 02:02:24',
'2016-07-08 02:02:46',
'2016-07-08 02:05:04',
'2016-07-08 02:11:26',
'2016-07-08 02:06:24',
'2016-07-08 02:04:32',
'2016-07-08 02:08:50',
'2016-07-08 02:08:27',
'2016-07-08 02:02:30',
'2016-07-08 02:03:59',
'2016-07-08 02:01:19',
'2016-07-08 02:02:09',
'2016-07-08 02:05:47',
'2016-07-08 02:02:36',
'2016-07-08 02:01:02',
'2016-07-08 02:02:58',
'2016-07-08 02:06:19',
'2016-07-08 02:02:34',
'2016-07-08 02:00:17',
'2016-07-08 02:10:03',
'2016-07-08 02:08:20',
'2016-07-08 02:02:36',
'2016-07-08 02:17:25',
'2016-07-08 02:07:19',
'2016-07-08 02:13:07',
'2016-07-08 02:03:51',
'2016-07-08 02:03:35',
'2016-07-08 02:14:53',
'2016-07-08 02:18:36']
相同的代碼:
for item in beginning_time_list:
for one_list in time_by_seconds:
if item in one_list:
print item
這是結果:
2016-07-08 02:17:42
2016-07-08 02:17:42
2016-07-08 02:17:42
2016-07-08 02:17:42
2016-07-08 02:05:35
2016-07-08 02:05:35
2016-07-08 02:03:22
2016-07-08 02:26:33
2016-07-08 02:26:33
2016-07-08 02:26:33
2016-07-08 02:26:33
2016-07-08 02:26:33
2016-07-08 02:26:33
2016-07-08 02:14:54
2016-07-08 02:14:54
2016-07-08 02:14:54
2016-07-08 02:05:13
2016-07-08 02:05:13
2016-07-08 02:15:30
2016-07-08 02:15:30
2016-07-08 02:15:30
2016-07-08 02:15:30
2016-07-08 02:01:53
2016-07-08 02:02:31
2016-07-08 02:00:08
2016-07-08 02:04:16
2016-07-08 02:08:44
2016-07-08 02:08:44
2016-07-08 02:11:17
2016-07-08 02:11:17
2016-07-08 02:11:17
2016-07-08 02:01:40
2016-07-08 02:04:23
2016-07-08 02:01:34
2016-07-08 02:24:31
2016-07-08 02:24:31
2016-07-08 02:24:31
2016-07-08 02:24:31
2016-07-08 02:24:31
2016-07-08 02:00:27
2016-07-08 02:14:35
2016-07-08 02:14:35
2016-07-08 02:14:35
2016-07-08 02:00:57
2016-07-08 02:02:24
2016-07-08 02:02:46
2016-07-08 02:05:04
2016-07-08 02:05:04
2016-07-08 02:11:26
2016-07-08 02:11:26
2016-07-08 02:11:26
2016-07-08 02:06:24
2016-07-08 02:06:24
etc
對不起有95項!
有人知道我爲什麼有重複嗎? Thnx
好吧,但存儲每天在列表中的所有秒似乎不是一個好主意。當然有更好的方法。你究竟在努力實現什麼? –
我的猜測是你在'time_by_seconds'中有重複,即'2016-07-08 02:17:42'在多個列表中(我假設不應該發生) –