简体   繁体   English

如何以某种方式更改 python 字典中的所有键?

[英]How can I change all keys in certain way in python dictionary?

So I have this dictionary:所以我有这本字典:

sample = {"accessNumber":"0000093410-20-000010", "symbol": "CVX", "cik": "93410"}

I would like to take the keys of the dictionary and append the string "late_" to the beginning of every key.我想将字典的键和 append 的字符串“late_”带到每个键的开头。

so the dictionary will become like this:所以字典会变成这样:

sample = {"late_accessNumber":"0000093410-20-000010", "late_symbol": "CVX", "late_cik": "93410"}

Thanks!谢谢!

sample = {"accessNumber":"0000093410-20-000010", "symbol": "CVX", "cik": "93410"}

new_sample = {'late_'+key: value for key,value in sample.items()}
print(new_sample)

Output: Output:

{'late_accessNumber': '0000093410-20-000010', 'late_symbol': 'CVX', 'late_cik': '93410'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM