[英]Removing specific items from a dictionary in python?
I have a dictionary inside of a list and I want to remove the square brackets and single quotes inside of them.我在列表中有一本字典,我想删除其中的方括号和单引号。
Current Output:
[{'label': "['Tennessee'] 22960 Packages", 'value': 'TN'},
{'label': "['Illinois'] 6277 Packages", 'value': 'IL'},
{'label': "['California'] 4 Packages", 'value': 'CA'},]
Desired Output:
[{'label': "Tennessee 22960 Packages", 'value': 'TN'},
{'label': "Illinois 6277 Packages", 'value': 'IL'},
{'label': "California 4 Packages", 'value': 'CA'},]
Try this with a for loop:用 for 循环试试这个:
m[0]['label'] = m[0]['label'].replace("['", "").replace("']", "")
Result:结果:
[{'label': 'Tennessee 22960 Packages', 'value': 'TN'},
{'label': "['Illinois'] 6277 Packages", 'value': 'IL'},
{'label': "['California'] 4 Packages", 'value': 'CA'}]
With for loop:使用 for 循环:
m = [{'label': "['Tennessee'] 22960 Packages", 'value': 'TN'},
{'label': "['Illinois'] 6277 Packages", 'value': 'IL'},
{'label': "['California'] 4 Packages", 'value': 'CA'},]
for i in range(0, 3):
m[i]['label'] = m[i]['label'].replace("['", "").replace("']", "")
print(m)
Try this:尝试这个:
def remover(x):
x['label'] = x['label'].replace("['", "").replace("']", "")
return x
mylist = [{'label': "['Tennessee'] 22960 Packages", 'value': 'TN'},
{'label': "['Illinois'] 6277 Packages", 'value': 'IL'},
{'label': "['California'] 4 Packages", 'value': 'CA'},]
mylist = list(map(remover,mylist))
print (mylist)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.