I have a nested list a:
a = [['fuzzy', '12'], ['drake', '6'], ['lebron', '23'], ['davis', '3'], ['jason', '16'], ['jon', '33'], ['jeff', '20'], ['rich', '33'], ['big', '100'], ['small', '2'], ['jack', '11'], ['queen', '12'], ['king', '13']]
I need to loop through this nested list and only change the integers strings into integers and the output should show
[['fuzzy', 12], ['drake', 6]....]
Any ideas?
您可以使用map
功能:
a = map(lambda x:[x[0], int(x[1])], a)
使用列表理解,
result = [[item[0], int(item[1])] for item in a]
for i in range(len(a)): a[i][1] = int(a[i][1])
只需运行一个循环并用整数更改值
Do a for loop to run through each list, then reassign the second item in the list:
for pair in a:
pair[1] = int(pair[1])
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.