[英]Python3: Integer not repeated
Out of a random list of integers, with integers being repeated in the list, what is the way to print that integer out of the list which is not repeated at all? 在随机的整数列表中,如果在列表中重复整数,那么从列表中完全不重复打印该整数的方法是什么?
I have tried to solve the question by making the following program: 我试图通过编写以下程序来解决该问题:
K = int(input())
room_list = list(input().split())
room_set = set(room_list)
for i in room_set:
count = room_list.count(i)
if count == 1:
i = int(i)
print(i)
break
K being the number of the elements in the list. K是列表中元素的数量。
When I try to run the above program, it works well in the case of less elements however, when it is tested with a list having (say, 825) elements, the program times out. 当我尝试运行上述程序时,它在元素较少的情况下运行良好,但是,当使用包含(例如825个)元素的列表进行测试时,该程序会超时。
Please help me in optimizing the above code. 请帮助我优化上面的代码。
Elements whose repetition count in the list is one will be your answer. 列表中重复计数为1的元素将是您的答案。
from collections import Counter
a = [1,1,1,2,2,3,4,5,5]
c = Counter(a) # O(n)
x = [key for key, val in c.items() if val == 1]
print(x)
Output: 输出:
[3,4]
Counter class creates a dictionary of elements and repetitions by iterating through the list once that takes time O(n) and each element's access takes O(1) time. 计数器类通过一次遍历列表来创建元素和重复字典,这需要花费时间O(n),而每个元素的访问都需要O(1)时间。
The count function of the list iterates every time you call it on a list. 列表的计数功能在您每次在列表上调用时都会迭代。 In your case taking O(n^2) time. 在您的情况下,需要O(n ^ 2)时间。
This will print the number that occured least often: 这将打印最不经常出现的号码:
data = [3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,93,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9]
from collections import Counter
# least often
print (Counter(data).most_common()[-1][0])
# all non-repeated onces
# non_repeat = [k[0] for k in Counter(data).most_common() if k[1] == 1]
Output: 输出:
93
It uses a specialized dictionary: collection.Counter thats built for counting things in iterable you give it. 它使用专门的字典: collection.Counter,用于为可迭代的事物计数。
The method .most_common()
returns a sorted list of tuples of (key, count) - by printing its last member you get the one thats least often. 方法.most_common()
返回一个(key,count)元组的排序列表-通过打印它的最后一个成员,您得到的那个成员最少。
The built-up dict looks like this: 内置字典如下所示:
Counter({4: 4, 5: 4, 6: 4, 7: 4, 8: 4, 3: 3, 9: 3, 0: 2, 1: 2, 2: 2, 93: 1})
A similar approach is to use a collections.defaultdict and count them yourself, then get the one with the minimal value: 一种类似的方法是使用collections.defaultdict自己进行计数,然后以最小的值获得一个:
from collections import defaultdict
k = defaultdict(int)
for elem in data:
k[elem] += 1
print( min(k.items(), key=lambda x:x[1]) )
The last solutions is similar in approach without the specialized Counter - the advantage of both of them is that you iterate once over the whole list and increment a value instead of iterating n times over the whole list and count each distinct elements occurences once. 最后一种解决方案在方法上类似,但没有专门的计数器-两者的优点是,您可以对整个列表进行一次迭代并增加一个值,而不是对整个列表进行n次遍历,并且对每个不同的元素进行一次计数。
count()
on a list of pure distinct elements would lead to n
counting-runs through n
elements = n^2
actions needed. 在纯净的不同元素列表上使用count()
会导致通过n
元素= n^2
所需的动作进行n
计数。 n
actions needed. 字典方法只需要通过列表一次,因此只需要n
动作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.