如何检查列表中的两个连续数字(整数)是否具有相同的值? [英] How do I check if two consecutive numbers (integers) in a list have the same value?

查看:185
本文介绍了如何检查列表中的两个连续数字(整数)是否具有相同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字列表(列表的长度存储在一个单独的变量中),需要检查是否有任何连续的整数相同,然后打印是否相同.我该怎么办?

I have a list of numbers (with the length of the list stored in a separate variable) and need to check if any consecutive integers are the same and then print a message if they are. How would I do this?

推荐答案

尝试以下操作:使用 itertools.groupby

>>> import itertools
>>> your_list = [4, 5, 5, 6]
>>> for x, y in itertools.groupby(your_list):
...     if len(list(y))>=2:
...         print x, "they are consecutive"
... 
5 they are consecutive

这篇关于如何检查列表中的两个连续数字(整数)是否具有相同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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