In Dictionary, Converting the Value from String to Integer

In dictionary, converting the value from string to integer

dict_with_ints = dict((k,int(v)) for k,v in dict_with_strs.iteritems())

Django convert String in Dictionary to Integer for the first 3 keys

dict1 = {'id':'1', 'Book':'21', 'Member':'3', 'Title':'Chameleon vol. 2', 'Author':'Jason Bridge'}

y_dict = dict(list(dict1.items())[:3])
print(y_dict) #dict sliced to the first 3 items that their values will be converted
z_dict = dict(list(dict1.items())[3:])
print(z_dict) #the rest of item that their values will not be converted to integer
x_dict = {k:int(v) for k, v in y_dict.items()}
print(x_dict) # dict values converted to integer
w_dict = {**x_dict, **z_dict}
print(w_dict) # merge of first 3 items with values as integer and the rest of the dict intact

w_dict is the result you are looking for.

How to convert a dictionary value from a string to an int (Or how do I better write my code read it the value as an integer) in python

After you create ClassDict you can convert all values to integers, for example:

ClassDict = {k: int(v) for k, v in ClassDict.items()}
print(ClassDict)

Prints:

{'CSCI 160': 4, 'CSCI 289': 3, 'EE 201': 4, 'MATH 208': 3}

how do i convert the value from str to int in my dict

Try this

a = {'A': '120', 'B': '100', 'C': '100', 'D': '100'}

for x in a.keys():
a[x] = int(a[x])

or this

a = {'A': '120', 'B': '100', 'C': '100', 'D': '100'}
a = {k : int(v) for k, v in a.items()}

Converting dictionary values in python from str to int in nested dictionary

You have to go deeper in this case:

In [4]: {k: {kk: int(vv) for kk, vv in v.items()}
for k, v in series.items()}
Out[4]:
{'a': {'bar': 3, 'baz': 7, 'foo': 2, 'qux': 1},
'b': {'bar': 4, 'baz': 3, 'foo': 6, 'qux': 0},
'c': {'bar': 5, 'baz': 1, 'foo': 4, 'qux': 6}}

Your own example just iterates the key, value -pairs of series, which has (nested) dict values.

Note that this is not a generic solution. For that you'd need recursion or a stack.

Converting the value from string to integer in a nested dictionary

To build on Chris' answer,

>>> myDict = {'Sun': {'Object': 'Sun', 'Satellites': 'Mercury,Venus,Earth,Mars,Jupiter,Saturn,Uranus,Neptune,Ceres,Pluto,Haumea,Makemake,Eris', 'Orbital Radius': '0', 'RootObject': 'Sun', 'Radius': '20890260'}, 'Moon': {'Object': 'Moon', 'Orbital Radius': '18128500', 'Period': '27.321582', 'Radius': '1737000.10'}, 'Earth': {'Object': 'Earth', 'Satellites': 'Moon', 'Orbital Radius': '77098290', 'Period': '365.256363004', 'Radius': '6371000.0'}}
>>> for body in myDict:
... myDict[body]['Radius'] = float(myDict[body]['Radius'])
... myDict[body]['Orbital Radius'] = float(myDict[body]['Orbital Radius'])
>>> myDict
13: {'Earth': {'Object': 'Earth',
'Orbital Radius': 77098290.0,
'Period': '365.256363004',
'Radius': 6371000.0,
'Satellites': 'Moon'},
'Moon': {'Object': 'Moon',
'Orbital Radius': 18128500.0,
'Period': '27.321582',
'Radius': 1737000.1},
'Sun': {'Object': 'Sun',
'Orbital Radius': 0.0,
'Radius': 20890260.0,
'RootObject': 'Sun',
'Satellites': 'Mercury,Venus,Earth,Mars,Jupiter,Saturn,Uranus,Neptune,Ceres,Pluto,Haumea,Makemake,Eris'}}


Related Topics



Leave a reply



Submit