对于熟悉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'在多个列表中(我假设不应该发生) –