繁体   English   中英

从嵌套列表中删除重复项

[英]Removing duplicates from a nested list

我正在尝试获取一个名为new_colors的新列表,其中只有[['orange', 'green'], ['purple', 'red']] ,从我的原始列表colors中删除重复项。 例如, 'orange'在两个不同的列表中重复两次。

colors = [
    ['orange', 'green'],
    ['orange', 'yellow'],
    ['purple', 'red'],
    ['brown', 'red']]

这是我想出的,但它不起作用。

new_colors = []

for i in colors:
  if i not in new_colors:
    new_colors.append(i)

print(new_colors)

您可以定义和顺序更新一个set seen存储元素 seen ,并与any结合使用in测试子列表是否有任何seen的元素:

colors = [['orange', 'green'], ['orange', 'yellow'], ['purple', 'red'], ['brown', 'red']] 

seen = set()
output = []
for sublst in colors:
    if not any(x in seen for x in sublst):
        output.append(sublst)
        seen.update(sublst)

print(output) # [['orange', 'green'], ['purple', 'red']]

由于您的示例模棱两可,因此我在这里提供了一种解决方案,可以在“列”上独立跟踪重复项。 这意味着,如果您有一个额外的['red', 'black'] ,它将被保留,因为red在第一列中是唯一的。

new_colors = []
seen = [set() for i in range(len(colors))]

for l in colors:
    # check if any item was already picked
    if any(e in s for e,s in zip(l,seen)):
        continue
    new_colors.append(l)
    # update picked items
    for e,s in zip(l,seen):
        s.add(e)

print(new_colors)

Output:

[['orange', 'green'],
 ['purple', 'red']]

或多或少基于@j1-lee 的回答,您可以将整个东西包装在一个生成器中。 迭代颜色组,并为每个颜色组生成一组。 如果当前颜色集与之前看到的所有颜色集 colors 之间存在交集,则什么也不做。 否则,产生当前组并更新之前看到的 colors 的集合:

colors = [
    ['orange', 'green'],
    ['orange', 'yellow'],
    ['purple', 'red'],
    ['brown', 'red']
]

def to_filtered(groups):
    seen = set()
    for current_set, current_group in zip(map(set, groups), groups):
        if not seen & current_set:
            yield current_group
            seen |= current_set

print(list(to_filtered(colors)))

Output:

[['orange', 'green'], ['purple', 'red']]

你可以试试这个

colors = [
    ['orange', 'green'],
    ['orange', 'yellow'],
    ['purple', 'red'],
    ['brown', 'red']]
    
new_colors = []
isUnique = True

for pair in colors:
    unique_colors = [new_color for new_pair in new_colors for new_color in new_pair]
    for color in pair:
        if color in unique_colors:
            isUnique = False
            break
    if isUnique == True:
        new_colors.append(pair)
    isUnique = True

print(new_colors)

因此,首先我在这里声明一个附加变量isUnique作为标志,并使用外部 for 循环内的新列表unique_colors ,它是new_colors列表的扁平版本,每次将新的唯一颜色pair添加到new_colors列表时都会更新。

然后在内部 for 循环中,对当前pair的每种颜色进行检查。 在这里,如果当前pair的任何colorunique_colors列表的任何颜色匹配, isUnique将设置为False ,并且内部循环将中断。

之后isUnique将被检查,如果它是True那么当前pair将被添加到new_colors并且最后isUnique将被设置为True用于下一次迭代。

Output: [['orange', 'green'], ['purple', 'red']]

您必须嵌套 for 循环来迭代元素而不是子列表

colors = [
  ['orange', 'green'],
  ['orange', 'yellow'],
  ['purple', 'red'],
  ['brown', 'red']
]

new_colors = []

for i in colors:
  for j in i:
    if j not in new_colors:
      new_colors.append(j)

print(new_colors)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM