简体   繁体   English

列表中每个值的出现次数是唯一的

[英]the number of occurrences of each value in the list is unique

How can I solve this problem in python.如何在 python 中解决这个问题。 given a list of integers arr , write a function that returns true if and only if the number of occurrences of each value in the list is unique in python给定一个整数列表arr ,编写一个 function 当且仅当列表中每个值的出现次数在 python 中唯一时才返回true

So所以

a=[1, 2, 2, 3, 3, 3] True a=[1, 2, 2, 3, 3, 3] 真

Because number of 1s is 1, number of 2s are 2,,,因为 1 的数量是 1,所以 2 的数量是 2,,,

a=[1, 2, 3, 3] False a=[1, 2, 3, 3] 假

Because number of 1s is 1, number of 2s is 1因为 1 的数量是 1,所以 2 的数量是 1

def check_it(arr):
    occ = [arr.count(e) for e in set(arr)]
    return len(set(occ)) == len(occ)

Seen on a different post: Checking if all elements in a list are unique在另一篇文章中看到: 检查列表中的所有元素是否都是唯一的

def func(arr):
     return len(arr) == len(set(arr)):

Suppose you have code like:假设您有如下代码:

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9,]
arr2 = [1, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9,]

def contains_no_duplicates(arr):
    return len(arr) == len(set(arr))

print(contains_no_duplicates(arr))
print(contains_no_duplicates(arr2))

With output:使用 output:

True
False

Taking the set of a list removes duplicates.取一set列表会删除重复项。 This happens because each entry in a set must be unique in the same way that a dict cannot have a duplicate key.发生这种情况是因为集合中的每个条目都必须是唯一的,就像dict不能有重复键一样。

The naïve way is to put them all in a set, and rely on the set uniqueness guarantee to do the work.天真的方法是将它们全部放在一个集合中,并依靠集合的唯一性保证来完成工作。

If your list is huge, you're making a copy if it all, even if the first two elements are the same and would make the test fail.如果您的列表很大,那么即使前两个元素相同并且会使测试失败,您也要制作一份副本。 It's a really good, Pythonic solution.这是一个非常好的 Pythonic 解决方案。 Nice, moctarjallo.不错,莫克塔加洛。

Another way is to use the set-hashed-lookup feature to quit early.另一种方法是使用 set-hashed-lookup 功能提前退出。

def all_unique(a):
    found = set()
    for item in a:
        if item in found:
            return False
        found.add(item)
    return False

The problem is to write a function that returns true if the number of occurrences of each value in the list is unique.问题是编写一个 function 如果列表中每个值的出现次数是唯一的,则返回 true。 So, for example所以,例如

a=[1, 2, 2, 3, 3, 3] True number of 1s is 1, number of 2s are 2,,, a=[1, 2, 2, 3, 3, 3] 真1的个数是1,2的个数是2,,,

a=[1, 2, 3, 3] False number of 1s is 1, number of 2s is 1 a=[1, 2, 3, 3] 假 1 的个数为 1,2 的个数为 1

For the occurences of the list to be unique we must find all the occurences.为了使列表的出现是唯一的,我们必须找到所有的出现。 We can use the Counter from collections module for that.为此,我们可以使用 collections 模块中的计数器。 Then use a set to find unique elements and check its size.然后使用集合查找唯一元素并检查其大小。

from collections import Counter
ar  = [1,2]
ar2 = [1,2,2,3,3,3]
def is_unique(ar):
    occurence_list = Counter(ar).values()
    return len(occurence_list) == len(set(occurence_list))
print(is_unique(ar))
print(is_unique(ar2))

OUTPUT OUTPUT

False
True

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

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