简体   繁体   English

如何确定列表中是否有任何值超过两次?

[英]How to determine if any value occurs more than twice in a list?

I have a list and would like to determine if any value occurs more than twice. 我有一个列表,并想确定是否有任何值出现两次以上。 I've tried using collections and counter but I cannot get those to evaluate to a single True or False value. 我已经尝试过使用集合和计数器,但我无法将它们评估为单个True或False值。

myArray=[1,1,1,1,1,1,1,1,2]

I would like it to return: True if any value occurs more than twice . 我希望它返回:如果任何值出现超过两次,则为True

Any help is appreciated and it would really help if the solution was fast. 任何帮助都表示赞赏,如果解决方案很快就会有所帮助。 I'm checking hundreds of thousands of lists. 我正在检查数十万个列表。 I'm new to programming and this is my first post. 我是编程新手,这是我的第一篇文章。

EDIT: my attempts, also I'm new to stackoverflow UI 编辑:我的尝试,我也是stackoverflow UI的新手

import collections

arr= [1,2,3,5,6]

Counter(arr)

Returns: Counter({1: 1, 2: 1, 3: 1, 5: 1, 6: 1}) 返回: Counter({1: 1, 2: 1, 3: 1, 5: 1, 6: 1})

You can use collections.Counter for this: 您可以使用collections.Counter

from collections import Counter
print any(count > 2 for count in Counter(myArray).itervalues())   # True

Or, if you are using Python 3: 或者,如果您使用的是Python 3:

from collections import Counter
print(any(count > 2 for count in Counter(myArray).values()))   # True

You could always construct a histogram of the values and see if any entry is greater than two. 您始终可以构造值的直方图,并查看是否有任何条目大于2。 It could look something like this: 它可能看起来像这样:

def is_more_than_twice(l):
   hist = {}
   for el in l:
       if el in hist:
           hist[el] += 1
       else:
           hist[el] = 1
       if hist[el] > 2:
           return True
   return False

You don't need to iterate until the end of the list, just until you've met the condition of seeing an element el appearing more than twice. 您不需要迭代直到列表的末尾,直到您遇到看到元素el出现两次以上的情况。

Here's one way using collections.defaultdict . 这是使用collections.defaultdict的一种方法。 Like @HoriaComan's method, this solution doesn't require iterating your entire list. 与@ HoriaComan的方法一样,此解决方案不需要迭代整个列表。

myArray = [1,1,1,1,1,1,1,1,2]

from collections import defaultdict

def count_limit(L, k=2):
    d = defaultdict(int)
    for item in L:
        if d[item] == k:
            return True
        else:
            d[item] += 1
    return False

res = count_limit(myArray)  # False

Performance benchmarking 绩效基准

To demonstrate the impact, we can compare with Counter on a larger iterable: 为了证明这种影响,我们可以在更大的迭代Counter上与Counter进行比较:

myArray = [1,1,1,1,1,1,1,1,2]*10000

from collections import defaultdict, Counter

def count_limit(L, k=2):
    d = defaultdict(int)
    for item in L:
        if d[item] == k:
            return True
        else:
            d[item] += 1
    return False

def count_limit_counter(myArray):
    return any(count > 2 for count in Counter(myArray).values())

%timeit count_limit(myArray)          # 1.52 µs per loop
%timeit count_limit_counter(myArray)  # 6.64 ms per loop

Try it using set() 尝试使用set()

def OccursMoreThanTwice(myArray):
    for e in myArray:
        if myArray.count(e) > 2:
           return True
    return False

print OccursMoreThanTwice([1,1,1,1,1,1,1,1,2])

暂无
暂无

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

相关问题 在Python 2.6中是否有任何函数可以找到列表的模式(如果最多出现一个,则为多个)? - Is there any function to find the mode (or multiple if more than 1 value occurs most) of a list in Python 2.6? 获取列表中仅出现两次不超过两次的元素的值 - get the values of elements in the list which occurs only twice not more than twice Python:检查列表的最后一个值是否出现多次 - Python: check if last value of list occurs more than once 如何检查熊猫数据框中列中的元素是否在该列中出现两次或两次以上 - How to check if an element in a column in a pandas data frame occurs twice or more than twice in that column 显示特定列中任何值出现多次的行 - Display rows where any value in a particular column occurs more than once 如何找到一个单词(字符串)是否在 python 的输入/列表中出现多次 - How can I find if a word (string) occurs more than once in an input/list in python 如果最大的数字在同一个列表中多次出现,我如何将它们全部打印出来? - If the largest number occurs more than once in the same list, how do I print them all out? 对于给定的 bin,如何确定一个数组中的任何值是否低于另一个数组中的任何值? - How to determine if any value in one array, is lower than any value in another array, for a given bin? 我如何确定一个对象是否在列表中出现两次(Python) - How do i find if an object occurs twice in a list (Python) 如何在字符串内多次出现的字符上拆分字符串 - How to split a string on a character that occurs more than once inside the string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM