如何在Python中比较数组中的值-找出2个值是否相同 [英] How to compare values within an array in Python - find out whether 2 values are the same

查看:945
本文介绍了如何在Python中比较数组中的值-找出2个值是否相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上有一个由50个整数组成的数组,我需要找出这50个整数是否相等,如果相等,则需要执行一个操作.

I basically have an array of 50 integers, and I need to find out whether any of the 50 integers are equal, and if they are, I need to carry out an action.

我将如何去做?据我所知,Python中目前没有函数吗?

How would I go about doing this? As far as I know there isn't currently a function in Python that does this is there?

推荐答案

如果您的意思是您有一个列表,并且想知道是否有重复的值,请从列表中设置一个值,看看它是否短于该值.列表:

If you mean you have a list and you want to know if there are any duplicate values, then make a set from the list and see if it's shorter than the list:

if len(set(my_list)) < len(my_list):
    print "There's a dupe!"

但是,这不会告诉您重复值是什么.

This won't tell you what the duplicate value is, though.

这篇关于如何在Python中比较数组中的值-找出2个值是否相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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