Elegant Format for a MAC Address in Python 3.2

后端 未结 8 2156
无人及你
无人及你 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:23

    The easiest solution here is just to use str.join()

    >>> ":".join(str_grouper(2, "00233a990c21"))
    '00:23:3a:99:0c:21'
    

    Here using a modified version of the grouper() recipe from the itertools docs:

    def str_grouper(n, iterable):
         args = [iter(iterable)] * n
         for part in zip(*args): #itertools.izip in 2.x for efficiency.
             yield "".join(part)
    

提交回复
热议问题