string - Python split and manipulate results -



string - Python split and manipulate results -

what pythonic way of code below, copied php?

def get_extra_info(self, info): = [] in info.split(';'): t = i.split(':') extra[t[0]] = t[1] homecoming

info in next format

info = "test:1;xxx:4;yyy:12"

you can utilize generator look dict() function:

return dict(item.split(':', 1) item in info.split(';'))

this makes utilize of fact dict() function accepts iterable of (key, value) pairs. 1 argument str.split() limits splitting once; more efficient , : colons ignored , become part of value.

demo:

>>> info = "test:1;xxx:4;yyy:12" >>> dict(item.split(':', 1) item in info.split(';')) {'test': '1', 'xxx': '4', 'yyy': '12'}

python string split

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -