How to split value where is comma ?

Rustom Mody rustompmody at gmail.com
Fri Sep 9 02:31:55 EDT 2016


On Friday, September 9, 2016 at 11:51:25 AM UTC+5:30, Rustom Mody wrote:
> And if the original 'a' looked something like
> >>> a="p:1,q:2,r:42"
> 
> then you probably want something like:
> 
> >>> {k:v for item in a.split(',') for k,v in [item.split(':')]}
> {'q': '2', 'p': '1', 'r': '42'}
> >>>

Well if the a did look like
>>> a="a:1,b:2,c:42"

what we would need is likely to be
>>> {k:int(v) for item in a.split(',') for k,v in [item.split(':')]}
{'a': 1, 'c': 42, 'b': 2}

Likely, but not necessary -- eg we would not want leading zeroes in a 
phone number to disappear



More information about the Python-list mailing list