반응형

[Python] In dictionary, converting the value from string to integer. 

Dict에서 value가 숫자형이 아닐때 숫자로 변경

Taking this below example :

'user_stats': {'Blog': '1',
                'Discussions': '2',
                'Followers': '21',
                'Following': '21',
                'Reading': '5'},

I want to convert it into:

'Blog' : 1 , 'Discussion': 2, 'Followers': 21, 'Following': 21, 'Reading': 5

 

>>> d = {'Blog': '1', 'Discussions': '2', 'Followers': '21', 'Following': '21', 'Reading': '5'}
>>> dict((k, int(v)) for k, v in d.iteritems())
{'Blog': 1, 'Discussions': 2, 'Followers': 21, 'Following': 21, 'Reading': 5}

 

https://stackoverflow.com/questions/9224385/in-dictionary-converting-the-value-from-string-to-integer

 

In dictionary, converting the value from string to integer

Taking this below example : 'user_stats': {'Blog': '1', 'Discussions': '2', 'Followers': '21', 'Following': '21', 'Reading': '5'}, ...

stackoverflow.com

 

반응형

+ Recent posts