list = [
{'rank': '1', 'name': 'Gus’s World Famous Fried Chicken', 'stars': '4.0', 'numrevs': '549', 'price': '$$'},
{'rank': '2', 'name': 'South City Kitchen - Midtown', 'stars': '4.5', 'numrevs': '1777', 'price': '$$'}
]
I need to update numrevs
to int
.
I have this so far, but I can't get it to work:
list = [dt.update({k : int(v)}) for dt in rankings for k['numrevs'], v in dt.items()]
Basically you can do it quite easy with that:
for d in mylist:
d["numrevs"] = int(d["numrevs"])
As a remark: Don't name any variable list
. list
is a built-in function and to overwrite this function can only lead to problems therefore I renamed the list to mylist
.
For each dict in your list, create a new dict, where the value for each key is the same as the current dict, except for the values that you want to replace with an int.
Here I am calling the current list list1
to avoid the confusion between the variable name and the built-in type name.
list2 = [ {k:(int(v) if k=='numrevs' else v)
for k,v in d.items()}
for d in list1 ]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.