【问题标题】:Duplicates / common elements between two lists两个列表之间的重复/公共元素
【发布时间】:2016-09-21 10:32:09
【问题描述】:

对于熟悉 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 件商品!

有人知道为什么我有重复吗? 谢谢

【问题讨论】:

  • 好的,但是将一天中的所有秒数存储在列表中似乎不是一个好主意。当然有更好的方法。你到底想达到什么目标?
  • 我的猜测是您在time_by_seconds 中有重复项,即2016-07-08 02:17:42 在多个列表中(我认为这不应该发生)

标签: python list python-2.7 for-loop


【解决方案1】:

为了在两个列表中找到共同的元素,你可以使用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]

【讨论】:

  • 更新了列表列表的答案
【解决方案2】:

在其中一个子列表中找到该项目后,将继续搜索其他子列表。

您应该考虑使用break,一旦在其中一个子列表中找到当前日期项,就停止搜索它:

for item in beginning_time_list:
    for one_list in time_by_seconds:
        if item in one_list:
            print item
            break

【讨论】:

    【解决方案3】:
    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))
    

    【讨论】:

      猜你喜欢
      • 2018-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      • 2011-02-13
      • 1970-01-01
      相关资源
      最近更新 更多