遍历列表中的元组

时间:2018-09-13 21:10:56

标签: list tuples comparison python-3.6

假设我有一个由元组组成的列表:

 stList = [('NJ', 'Burlington County', '12/21/2017'),
 ('NJ', 'Burlington County', '12/21/2017'),
 ('NJ', 'Burlington County', '12/21/2017'),
 ('VA', 'Frederick County', '2/13/2018'),
 ('MD', 'Montgomery County', '8/7/2017'),
 ('NJ', 'Burlington County', '12/21/2017'),
 ('NC', 'Lee County', '1/14/2018'),
 ('NC', 'Alamance County', '11/28/2017'),]

我要遍历每个项目(元组),如果已经存在,请将其从stList中删除。

for item in stList:
    if item in stList:
        stList.remove(item)

这并不完全有效。基本上,当我运行它时,如果元组中的任何项也在列表中,它将删除该项,所以我得到了:

[('NJ', 'Burlington County', '12/21/2017'),
 ('VA', 'Frederick County', '2/13/2018'),
 ('NJ', 'Burlington County', '12/21/2017'),
 ('NC', 'Alamance County', '11/28/2017')]

有什么更好的方法来解决这个问题?

1 个答案:

答案 0 :(得分:1)

您可以直接比较元组。

所有条目都匹配的双胞胎将被视为相等。

>>> ('NJ', 'Burlington County', '12/21/2017') == ('NJ', 'Burlington County', '12/21/2017')
>>> True

>>> ('NJ', 'Burlington County', '12/21/2017') == ('NJ', 'Burlington County', '1/21/2017')
>>> False

请勿从要迭代的集合中删除项目。

这可能会导致意外的行为,除非您知道删除的完成方式和正确的执行方式。那是另一个故事。

以下是一些选择。

seen = set()
result = []
for item in stList:
    # Tuple can be compared directly to other tupled in `seen`.
    if item not in seen:
        seen.add(item)
        result.append(item)

stList = result

另一种可能性是

seen = set()
# Use a list to preserve ordering. Change to set if that does not matter.
first_seen = []
for i, item in enumerate(stList):
    if item not in seen:
        seen.add(item)
        first_seen.append(i)

stList = [stList[i] for i in first_seen]

修改 出于第二种考虑,除非您出于某种原因需要索引(即可以将它们重新用于其他任务),否则第二种选择不如第一种选择,因为在第一种情况下,result存储引用而不是副本元组,因此与在stList中存储这些元组的索引时,将或多或少地占用相同的内存。

如果订购无所谓

stList = list(set(stList))

如果您只想迭代并且不需要索引stList,那么您甚至可以将其保留为set对象。

相关问题