Elegant Format for a MAC Address in Python 3.2

后端 未结 8 2158
无人及你
无人及你 2020-12-19 00:46

I am looking for a more elegant solution to formatting a MAC address with colons. I am using Python 3.2. A fancy list comprehension perhaps?

s=\"\"
h=\"0023         


        
相关标签:
8条回答
  • 2020-12-19 01:24

    Not new but still most elegant I think:

    import re
    ':'.join(re.findall('..', '08002714f616'))
    
    0 讨论(0)
  • 2020-12-19 01:26
    >>> import itertools
    >>> h = '00233a990c21'
    >>> ':'.join(a+b for a, b in (itertools.izip(
    ...                              itertools.compress(h, itertools.cycle((1,0))),
    ...                              itertools.compress(h, itertools.cycle((0,1))))))
    >>> '00:23:3a:99:0c:21'
    

    Does that win for the highest density of parentheses?

    0 讨论(0)
提交回复
热议问题