[英]How can I figure out which arbitrary number occurs twice in a list of integers from input? (Python)
Say I'm receiving a list of arbitrary numbers from input, like假设我从输入中收到一个任意数字的列表,比如
[1,2,3,4,5,6,7,8,8,9,10]
My code doesn't know what numbers these are going to be before it receives the list, and I want to return the number that appears twice automatically.我的代码在收到列表之前不知道这些数字是什么,我想返回自动出现两次的数字。 How do I go about doing so?
我该怎么做?
Thank you.谢谢你。
You can use Counter By defualt Method in python 2 and 3您可以在 python 2 和 3 中使用 Counter By defualt 方法
from collections import Counter
lst=[1,2,3,4,5,6,7,8,8,9,10]
items=[k for k,v in Counter(lst).items() if v==2]
print(items)
You could do:你可以这样做:
input = [1,2,3,4,5,6,7,8,8,9,10]
list_of_duplicates = []
for i in input:
if i not in list_of_duplicates:
list_of_duplicates.append(i)
input.pop(i)
print(input)
Now input will have all the numbers that were in the list multiple times.现在输入将包含多次出现在列表中的所有数字。
Hope this helps.希望这可以帮助。
input = [1,2,3,4,5,6,7,8,8,9,10]
unique = set(input)
twice = []
for item in unique:
if input.count(item) == 2:
twice.append(item)
I've created something monstrous that does it in one line because my brain likes to think when it's time for bed I guess?我创造了一些可怕的东西,它在一行中完成,因为我的大脑喜欢思考什么时候该睡觉了?
This will return a list of all duplicate values given a list of integers.这将返回给定整数列表的所有重复值的列表。
dupes = list(set(map(lambda x: x if inputList.count(x) >= 2 else None, inputList))-set([None]))
How does it work?它是如何工作的? The map() function applies a function every value of a list, in your case our input list with possible duplicates is called "inputList".
map() 函数对列表的每个值应用一个函数,在您的情况下,我们的可能重复的输入列表称为“inputList”。 It then applies a lambda function that returns the value of the integer being iterated over IF the iterated value when applied to the inputList via the .count() method is greater than or equal to two, else if it doesn't count as a duplicate it will return None.
然后它应用一个 lambda 函数,该函数返回正在迭代的整数的值,如果通过 .count() 方法应用于 inputList 时的迭代值大于或等于 2,否则如果它不计为重复它将返回 None。 With this lambda function being applied by the map function, we get a list back that contains a bunch of None's and the actual integers detected as duplicates via the lambda function.
通过 map 函数应用这个 lambda 函数,我们得到一个包含一堆 None 和通过 lambda 函数检测为重复的实际整数的列表。 Given that this is a list, we the use set to de-duplicate it.
鉴于这是一个列表,我们使用 set 对其进行去重。 We then minus the set of duplicates against a static set made from a list with one item of None, stripping None values from our set of the map returned list.
然后,我们针对一个静态集合减去一组重复项,该静态集合由具有一项 None 的列表组成,从我们的映射返回列表集中剥离 None 值。 Finally we take the set after subtraction and convert it to a list called "dupes" for nice and easy use.
最后,我们将减法后的集合转换为一个名为“dupes”的列表,以便于使用。
Example usage...示例用法...
inputList = [1, 2, 3, 4, 4, 4, 5, 6, 6, 7, 1001, 1002, 1002, 99999, 100000, 1000001, 1000001]
dupes = list(set(map(lambda x: x if inputList.count(x) >= 2 else None, inputList))-set([None]))
print(dupes)
[1000001, 1002, 4, 6]
I'll let someone else elaborate on potential scope concerns..... or other concerns......我会让其他人详细说明潜在的范围问题......或其他问题......
This will create a list of the numbers that are duplicated.这将创建一个重复数字的列表。
x = [1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10]
s = {}
duplicates = []
for n in x:
try:
if s[n]:
duplicates.append(n)
s[n] = False
except KeyError:
s[n] = True
print(duplicates)
Assuming the list doesn't contain 0
假设列表不包含
0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.