将十六进制字符串拆分为Python中的列表 - 如何?

时间:2010-07-03 07:43:41

标签: python string

如果我有这个字符串:

hexstring = '001122334455'

如何将其拆分为列表,结果如下:

hexlist = ['00','11','22','33','44','55']

我想不出一个好的,pythonic的方法来做到这一点:/

7 个答案:

答案 0 :(得分:10)

>>> [hexstring[i:i+2] for i in range(0,len(hexstring), 2)]
['00', '11', '22', '33', '44', '55']

答案 1 :(得分:7)

可替换地:

>>> hexstring = "01234567"
>>> it=iter(hexstring); [a+b for a,b in zip(it, it)]
['01', '23', '45', '67']

如果您的目标是Python 2.x,请使用itertools.izip代替zip

此方法是itertools配方中grouper的特定版本。


一些微观基准:

$ 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

观察:

  • 使用Python 2.6上的长字符串,@SilentGhost和我的方法是最快的。当然你需要使用惰性迭代器,例如xrangeizip
  • 使用Python 2.6上的短字符串,@Nick的正则表达式最快。
  • 在Python 3.1上,我的方法在两种情况下都是最快的,但我相信这是因为Python 3.x的优化程度较低。
  • 当然,premature optimization is evil, etc.

答案 2 :(得分:2)

使用正则表达式:

>>> import re
>>> re.findall('.{1, 2}', '001122334455')
['00', '11', '22', '33', '44', '55']
>>> 

答案 3 :(得分:1)

有点奇怪的方式:

map(''.join,zip(hexstring[::2],hexstring[1::2]))

答案 4 :(得分:1)

hexstring = "01234567"
[''.join(x) for x in zip(*[iter(hexstring)]*2)]

答案 5 :(得分:0)

功能风格:

>>> result = map(lambda x,y : x+y, *[iter('0013A20040522BAA')]*2)
>>> tuple(result)
('00', '13', 'A2', '00', '40', '52', '2B', 'AA')

或之前建议的zip附近的其他变体:

>>> result = (a+b for a,b in zip(*[iter('0013A20040522BAA')]*2))
>>> tuple(result)
('00', '13', 'A2', '00', '40', '52', '2B', 'AA')

答案 6 :(得分:0)

怎么样?有些二进制文件有单个(),有些得到(\),有些\\和未知(十六进制?),如x99wh

(B' E!\ X00 \ X00(H \ XD2 @ \ X00 \ X80 \ X06 \ xa1J \ XC0 \ xa8 \ X01 = [\ X11 \ X8F \ XC5 [\ X01 \ XBB \ x99wh \ XCD \\ xb2 \ xcdQP \ x10 \ x82t)M \ x00 \ x00',(' 192.168.1.33',0))

在窗口

上使用socket.socket(socket.AF_INET,socket.SOCK_RAW)

这是linux示例。 http://www.binarytides.com/python-packet-sniffer-code-linux/