简体   繁体   English

如何从输入的整数列表中找出哪个任意数字出现两次? (Python)

[英]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.

相关问题 如何查找Python 3.3.0中用户输入是否包含在任意整数列表中 - How can I find if user input is inclusive in list of arbitrary integers in Python 3.3.0 如何通过从用户输入中删除哪个值来从(整数和字符串)列表中删除整数? - How can I delete integers from list (of integers and strings) by taking which value to delete from user input? 无法弄清楚如何在python中为列表中的元素分配一个数字 - Can't figure out how to assign a number to a element in a list in python 我如何确定一个对象是否在列表中出现两次(Python) - How do i find if an object occurs twice in a list (Python) Python:非常基础,无法弄清为什么它没有拆分为列出的较大数字,而是拆分为单个整数 - Python: Very Basic, Can't figure out why it is not splitting into the larger number listed but rather into individual integers 如何确定哪些参数映射到哪些变量? - How can I figure out which parameters map to which variables? 二维列表函数,我不知道如何打印输入,python - 2d list functions, i cant figure out how to print input, python 如何确定哪个版本的Python首先具有给定的Fix? - How can I figure out which version of Python first has a given Fix? Python:如何绘制可以自由放大/缩小的图形 - Python: how shall I plot a figure which can zoom in/out freely 确定哪些整数在 python 列表中顺序不正确 - determining which integers are out of sequence in a python list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM