检查十六进制值列表是否连续 [英] Check whether a list of hexadecimal values is sequential or not

查看:67
本文介绍了检查十六进制值列表是否连续的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要一个函数/语句,以检查mylist的所有值是否都是顺序的,即十六进制列表. 例如:

Want a function / statement, to check whether all the values of mylist are sequential or not, which is hexadecimal list. For example:

def checkmylist(mylist):
    #code returns True or False


mylist1 = ['03', '04', '05', '06', '07', '08', '09', '0a', '0b', '0c','0d', '0e', '0f']
mylist2 = ['03', '05', '06', '07', '08', '09', '0a', '0b', '0c','0d', '0e', '0f']

checkmylist(mylist1)
#expected to returns pass
checkmylist(mylist2)
#expected to returns fail

推荐答案

def checkmylist(mylist):
    it = (int(x, 16) for x in mylist)
    first = next(it)
    return all(a == b for a, b in enumerate(it, first + 1))

在第一个语句中,我们将十六进制数字转换为整数生成器.使用next(it),我们将生成器的第一个元素.然后,列举从first + 1开始编号的其余元素.我们得出的结论是,如果每个元素的编号与元素本身的编号相同,我们将获得一个顺序列表.

With the first statement we convert the hex numbers to a generator of integers. With next(it) we take the first element of the generator. Then we enumerate the rest of the elements starting the numbering from first + 1. We conclude that we have a sequential list if the numbering of each element is the same as the element itself.

这篇关于检查十六进制值列表是否连续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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