Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a hex string into a list in Python - How?

Tags:

python

string

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 :/

like image 874
dave Avatar asked Jul 03 '10 07:07

dave


People also ask

How do you split a string into a list in Python?

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.

How do I split a string into multiple characters Python?

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.

How do you convert hex to string in Python?

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.


3 Answers

>>> [hexstring[i:i+2] for i in range(0,len(hexstring), 2)]
['00', '11', '22', '33', '44', '55']
like image 54
SilentGhost Avatar answered Oct 06 '22 00:10

SilentGhost


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:

  • With long strings on Python 2.6, @SilentGhost's and my method are fastest. Of course you need to use lazy iterators e.g. xrange and izip.
  • With short strings on Python 2.6, @Nick's regular expression is fastest.
  • On Python 3.1 my method is fastest in both cases, but I believe it's because Python 3.x is less optimized.
  • Of course, premature optimization is evil, etc..
like image 31
kennytm Avatar answered Oct 05 '22 23:10

kennytm


A slightly odd way:

map(''.join,zip(hexstring[::2],hexstring[1::2]))
like image 20
Boojum Avatar answered Oct 06 '22 00:10

Boojum