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
Similar to @JuniorCompressor but using a list comprehension, your base construct would be:
You could check to see if that is true/false easy enough:
Or you can find the indexes where it is off (not sequential) with the help of numpy
You can use
iter
to create an iterator of your list (from second index to end) and then useall
function to check if you have a sequence, note thatint(next(it),16)
(or as a more efficient way as mentioned in comment usefunctools.partial(int, base=16)
)will convert your string to integer with base 16 then you can do operation on them :Demo:
One hack to do it is. This finds the total elements in the list. As you mention that it has t be sequential, the last element must be length of the list more than the first element.
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 fromfirst + 1
. We conclude that we have a sequential list if the numbering of each element is the same as the element itself.