[英]Duplicates / common elements between two lists
對於熟悉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件商品!
有人知道為什么我要重復嗎? n
為了在兩個列表中找到共同的元素,可以將set()
用作:
>>> a = [1, 2, 3, 4]
>>> b = [3, 4, 5, 6]
>>> list(set(a).intersection(set(b)))
[3, 4]
在您的情況下, b
是列表列表。 您需要首先弄平列表。 為此,您可以使用itertools.chain()
>>> from itertools import chain
>>> a = [1, 2, 3, 4]
>>> b = [[3, 5, 6], [4, 8, 9]]
>>> list(set(a).intersection(set(chain.from_iterable((b)))))
[3, 4]
在子列表之一中找到該項目后,搜索將與其他子列表一起進行。
一旦在子列表之一中找到當前日期項,就應該考慮使用break
來停止搜索:
for item in beginning_time_list:
for one_list in time_by_seconds:
if item in one_list:
print item
break
import collections
def flatten(iterable):
for item in iterable:
if isinstance(item, (str, bytes)):
yield item
if isinstance(item, collections.Sequence):
yield from flatten(item)
else:
yield item
a = [1, 6, 10]
b = [[0, 1, 2], 3, [4], [5, (6, 7), 8], 9]
common_items = set(a) & set(flatten(b))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.