Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中找到多个不重复的数字?_Python_Python 3.x - Fatal编程技术网

如何在python中找到多个不重复的数字?

如何在python中找到多个不重复的数字?,python,python-3.x,Python,Python 3.x,我有一个在列表中查找非重复元素的方法: def dupes(a): s = {} for ele in a: if ele not in s: s[ele] = 1 else: s[ele] += 1 for x in s: if s[x] == 1: return 'this is the only non-repeating element valu

我有一个在列表中查找非重复元素的方法:

def dupes(a):
    s = {}
    for ele in a:
        if ele not in s:
            s[ele] = 1
        else:
            s[ele] += 1
    for x in s:
        if s[x] == 1:
            return 'this is the only non-repeating element value is :', s[x], 'and the key is :', x
    return

l = [4, 7, 4, 5, 7, 6, 5, 6, 10]
cd = dupes(l)
print("This is dupes: ", cd)
代码成功运行,下面是输出:

This is dupes:  ('this is the only non-repeating element value is :', 1, 'and the key is :', 10)
但当我试图在列表中添加多个非重复元素时,输出不会改变。例如,如果我在列表末尾添加11,则输出仍然与上面相同


有什么想法吗?

您可以通过以下方法简洁地实现上述目标:

输出:

This is dupes:  [4, 7, 5, 6]
您遇到的问题是,您过早地从函数返回包含第一个副本的字符串。其余的副本丢失了

您已经有了重复列表,让我们只返回数据,这样我们就可以将结果的计算与结果的显示分开

return list(e for e in s if s[e] > 1)

可以使用列表获取值为1的所有键

nondupes = [k for (k,v) in s.items() if v==1]
return 'Non-repeating elements: ' + str(nondupes)
此外,您还可以将所有计数代码替换为集合。计数器:


这很好。

您可以在一行python代码中使用count函数轻松解决此问题:

l = [4, 7, 4, 5, 7, 6, 5, 6, 10 ,11]

only_one_time = [e for e in l if l.count(e) < 2]

print(only_one_time)

给定的代码返回它遇到的第一个非重复元素。 例如 如果输入列表为l=[12,11,4,7,4,5,7,6,5,6,10],
输出将是This is dupes:'这是唯一的非重复元素值为:',1',键为:',12。

实际上,当您在第10行返回时,函数结束。这是在你还不理解列表理解的情况下,因为很多人已经用这种技术为你提供了解决方案。我将给出一个简单的解决方案,它将返回一个非重复数字的列表

def dupes(a):
    s = {}
    non_dupes = []
    for ele in a:
        if ele not in s:
            s[ele] = 1
        else:
            s[ele] += 1
    for x in s:
        if s[x] == 1:
            non_dupes.append(x)
    return non_dupes

l = [4, 7, 4, 5, 7, 6, 5, 6, 10, 11]
cd = dupes(l)
print("This is dupes: ", cd)

请提供预期的价格。显示中间结果与预期结果的偏差。您的数据和流跟踪得到了什么?这是我们希望您提供的问题的基本调试。现在添加程序的输出当您找到第一个元素时,您将从函数返回。返回将退出循环和函数。首先尝试捕获所有值,然后使用return语句发送最终结果之所以只显示一个值,是因为如果找到一个元素,就返回或终止函数。将唯一元素添加到列表中,然后返回该列表,这是一个可能的解决方案。答案很好,尝试提供代码来解决问题。我想,我已经找到了我想要的,谢谢大家。反正不会有重复的,所以你不需要set@RufusVS我从编写的代码中推断出什么可能是适合学习的材料。考虑到这个主题是重复的,我想说它是相关的。@RufusVS我倾向于同意,我从答案中删除了set。@user3521180你能分享一下为什么选择这个答案而不是其他答案吗?
def dupes(a):
    s = {}
    for ele in a:
        if ele not in s:
            s[ele] = 1
        else:
            s[ele] += 1
    count = 0
    keys = []
    for x in s:
        if s[x] == 1:
            count += 1
            keys.append(x)
    return 'this is the only non-repeating element value is :', count, 'and the key is :', keys
l = [4, 7, 4, 5, 7, 6, 5, 6, 10 ,11]

only_one_time = [e for e in l if l.count(e) < 2]

print(only_one_time)
def dupes(a):
    s = {}
    non_dupes = []
    for ele in a:
        if ele not in s:
            s[ele] = 1
        else:
            s[ele] += 1
    for x in s:
        if s[x] == 1:
            non_dupes.append(x)
    return non_dupes

l = [4, 7, 4, 5, 7, 6, 5, 6, 10, 11]
cd = dupes(l)
print("This is dupes: ", cd)