If I have this string:
hexstring = '001122334455'
How can I split that into a list so the result is:
hexlist = ['00', '11', '22', '33', '44', '55']
I can't think of a nice, pythonic way to do this :/
The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.
Method 1: Split multiple characters from string using re. split() This is the most efficient and commonly used method to split multiple characters at once. It makes use of regex(regular expressions) in order to do this.
hex() function in Python hex() function is one of the built-in functions in Python3, which is used to convert an integer number into it's corresponding hexadecimal form. Syntax : hex(x) Parameters : x - an integer number (int object) Returns : Returns hexadecimal string.
>>> [hexstring[i:i+2] for i in range(0,len(hexstring), 2)]
['00', '11', '22', '33', '44', '55']
Alternatively:
>>> hexstring = "01234567"
>>> it=iter(hexstring); [a+b for a,b in zip(it, it)]
['01', '23', '45', '67']
Use itertools.izip
instead of zip
if you're targeting Python 2.x.
This method is a specific version of grouper
in the itertools recipe.
Some micro-benchmarks:
$ python2.6 -m timeit -s 'hexstring = "01234567"*500' '[hexstring[i:i+2] for i in xrange(0,len(hexstring), 2)]' 1000 loops, best of 3: 409 usec per loop $ python2.6 -m timeit -s 'hexstring = "01234567"*500' '[hexstring[i:i+2] for i in range(0,len(hexstring), 2)]' 1000 loops, best of 3: 438 usec per loop $ python2.6 -m timeit -s 'hexstring = "01234567"*500' 'it=iter(hexstring); [a+b for a,b in zip(it, it)]' 1000 loops, best of 3: 526 usec per loop $ python2.6 -m timeit -s 'hexstring = "01234567"*500; from itertools import izip' 'it=iter(hexstring); [a+b for a,b in izip(it, it)]' 1000 loops, best of 3: 406 usec per loop $ python2.6 -m timeit -s 'hexstring = "01234567"*500; import re; r=re.compile(".{1,2}"); f=r.findall' 'f(hexstring)' 1000 loops, best of 3: 458 usec per loop $ python3.1 -m timeit -s 'hexstring = "01234567"*500' '[hexstring[i:i+2] for i in range(0,len(hexstring), 2)]' 1000 loops, best of 3: 756 usec per loop $ python3.1 -m timeit -s 'hexstring = "01234567"*500' 'it=iter(hexstring); [a+b for a,b in zip(it, it)]' 1000 loops, best of 3: 414 usec per loop $ python3.1 -m timeit -s 'hexstring = "01234567"*500; import re; r=re.compile(".{1,2}"); f=r.findall' 'f(hexstring)' 1000 loops, best of 3: 865 usec per loop $ python2.6 -m timeit -s 'hexstring = "01234567"' '[hexstring[i:i+2] for i in xrange(0,len(hexstring), 2)]' 1000000 loops, best of 3: 1.52 usec per loop $ python2.6 -m timeit -s 'hexstring = "01234567"' '[hexstring[i:i+2] for i in range(0,len(hexstring), 2)]' 1000000 loops, best of 3: 1.76 usec per loop $ python2.6 -m timeit -s 'hexstring = "01234567"' 'it=iter(hexstring); [a+b for a,b in zip(it, it)]' 100000 loops, best of 3: 3.78 usec per loop $ python2.6 -m timeit -s 'hexstring = "01234567"; from itertools import izip' 'it=iter(hexstring); [a+b for a,b in izip(it, it)]' 100000 loops, best of 3: 2.39 usec per loop $ python2.6 -m timeit -s 'hexstring = "01234567"; import re; r=re.compile(".{1,2}"); f=r.findall' 'f(hexstring)' 1000000 loops, best of 3: 1.45 usec per loop $ python3.1 -m timeit -s 'hexstring = "01234567"' '[hexstring[i:i+2] for i in range(0,len(hexstring), 2)]' 100000 loops, best of 3: 2.46 usec per loop $ python3.1 -m timeit -s 'hexstring = "01234567"' 'it=iter(hexstring); [a+b for a,b in zip(it, it)]' 1000000 loops, best of 3: 1.84 usec per loop $ python3.1 -m timeit -s 'hexstring = "01234567"; import re; r=re.compile(".{1,2}"); f=r.findall' 'f(hexstring)' 100000 loops, best of 3: 2.07 usec per loop
Observation:
xrange
and izip
.A slightly odd way:
map(''.join,zip(hexstring[::2],hexstring[1::2]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With