返回在数组 python 中只出现一次的数字 [英] return the number that occur only once in a array python

查看:45
本文介绍了返回在数组 python 中只出现一次的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组 [1,2,3,4,5,6,1,2,3,4] 我想返回 5, 6.

I have an array [1,2,3,4,5,6,1,2,3,4] and I would like to return 5, 6.

如果我使用 set(my_array) 我得到 1,2,3,4,5,6.有没有一种pythonic的方法来做到这一点.谢谢.

If I use set(my_array) I get 1,2,3,4,5,6. Is there a pythonic way to do this. Thanks.

非常感谢任何帮助.

推荐答案

#List of data which has every item repeated except for 5 and 6
lst= [1,2,3,4,5,6,1,2,3,4]

#This list comprehension prints a value in the list if the value only occurs once.
print [x for x in lst if lst.count(x)==1]
#Output
[5, 6]

这篇关于返回在数组 python 中只出现一次的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆