简体   繁体   English

如何在新列表中获取列表的唯一编号?

[英]How to get the unique numbers of a list in a new list?

I have this list:我有这个清单:

list = [{1,2,3,4}, {3,4,5}, {2,6}] 

I want to have this as the output:我想把它作为输出:

{1,6}

So I only want the unique numbers to have an output.所以我只希望唯一的数字有输出。 This is what I tried, but it does not work:这是我尝试过的,但不起作用:

list = [{1,2,3,4}, {3,4,5}, {2,6}] 
s1 = []
for number in list:
   if number not in s1:
      s1.append(number)
def unique(s1):
   return set.difference(*s1)
print (unique(s1))

My output is:我的输出是:

{1, 2, 3, 4}

I have no clue how to fix this?我不知道如何解决这个问题? I am a python beginner so can anyone explain what the answer is and why that should be the solution?我是 python 初学者,所以任何人都可以解释答案是什么以及为什么这应该是解决方案? Many thanks in advance!提前谢谢了!

Use a Counter object.使用Counter对象。

>>> from collections import Counter
>>> from itertools import chain
>>> my_list = [{1,2,3,4}, {3,4,5}, {2,6}]
>>> set(k for k,v in Counter(chain.from_iterable(my_list)).items() if v == 1)
set([1, 5, 6])

chain.from_iterable "flattens" my_list . chain.from_iterable “扁平化” my_list The Counter compiles how many times each element from the flattened list is seen, and the generator expression sends only the keys mapped to a value of 1 to set . Counter编译扁平列表中每个元素被看到的次数,并且生成器表达式仅将映射到值 1 的键发送到set


Some of the intermediate values involved:涉及的一些中间值:

>>> list(chain.from_iterable(my_list))
[1, 2, 3, 4, 3, 4, 5, 2, 6]
>>> Counter(chain.from_iterable(my_list))
Counter({2: 2, 3: 2, 4: 2, 1: 1, 5: 1, 6: 1})

I had an earlier answer posted that wasn't right.我之前发布了一个不正确的答案。 I don't like to give up, and I wanted to find an answer to this question that only used sets.我不喜欢放弃,我想找到这个只使用集合的问题的答案。 I'm not saying that this is a better answer than the others, but it does achieve at least my own goal of not bringing any extra packages into the solution:我并不是说这比其他答案更好,但它至少实现了我自己的目标,即不将任何额外的包带入解决方案中:

def unique(data):
    result = set()
    dups = set()
    for s1 in data:
        # accumulate everything we see more than once
        dups = dups | (result & s1)
        # accumulate everything
        result = (result | s1)
    # the result is everything we only saw once, or in other words,
    # everything we saw minus everything we saw more than once
    return result - dups

print(unique([{1,2,3,4}, {3,4,5}, {2,6}]))
print(unique([{1}, {1}, {1}]))

Output:输出:

set([1, 5, 6])
set([])

There's probably a better solution even to using just sets.即使只使用集合,也可能有更好的解决方案。 I always want to try to use the simplest set of tools possible because I think it is the most understandable to do so, and will often turn out to be as efficient as anything else as well.我总是想尽可能使用最简单的工具集,因为我认为这样做是最容易理解的,并且通常会证明它与其他任何工具一样有效。

Not a fancy python solution but what you probably want is to build a dict where the key is a number in the list and the value is the number of times it occurs.不是一个花哨的 python 解决方案,但您可能想要的是构建一个字典,其中键是列表中的一个数字,值是它出现的次数。

This solution is by no way optimal since we are performing to many iterations:这个解决方案绝不是最优的,因为我们要执行多次迭代:

list = [{1,2,3,4}, {3,4,5}, {2,6}] 
count_hash = {}

for dict in list:
    for number in dict:
        if number in count_hash:
            count_hash[number] += 1
        else:
            count_hash[number] = 1

for number in count_hash:
    if count_hash[number] == 1:
        print(number)

I agree with chepner on using a Counter.我同意chepner关于使用计数器的意见。 Using functools .reduce to flatten the list came from here .使用functools .reduce 来压平来自这里的列表。

import functools
import operator
from collections import Counter

my_list = [{1,2,3,4}, {3,4,5}, {2,6}]
flat_list = functools.reduce(operator.iconcat, my_list, [])

results = [k for (k, v) in Counter(flat_list).items() if v == 1]

print(results)

# OUTPUT
# [1, 5, 6]

I didn't see any of the answers given that were like my solution.我没有看到任何类似于我的解决方案的答案。 I hope the symmetric difference of the sets given provides that correct manner (to this question).我希望给定集合的对称差异提供了正确的方式(对于这个问题)。

>>> L = [{1,2,3,4}, {3,4,5}, {2,6}]
>>> M = L[0]
>>> for s in L[1:]:
    M ^= s

    
>>> M
{1, 5, 6}

Note: The question was about lists but the list he gave was a list of sets.注意:问题是关于列表,但他给出的列表是一个集合列表。

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

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