python - Python list object mutation
问题描述
I have a python data structure which is a list of dict, like:
data = [
{
'user':{
'name': 'INIT_NAME'
'age': 18
},
'cart':{
...
}
},
...
]
my objective is to parse every single attr in the dict, so that i could replace the string with upper cap to a specified value. For example, i would like to parse the data structure above to:
data = [
{
'user':{
'name': 'my name'
'age': 18
},
'cart':{
...
}
},
...
]
of cause, i would do this by python code like:
for d in data:
for type, attrs in d.items():
for key, value in attrs.items():
attrs[key] = parse_attr(value)
def parse_attr(value):
if value == 'INIT_NAME':
return 'my name'
Sure it will work, but sames it is not quite readable and little hard to knowing what is going on by a glance.
I just wonder are there any simplier way better than this? I am care about code readability therefore i am looking for a better approach. I am just new to python and just know the basic like decorator and syntactic sugar which is not helping in this case. Please let me know if you have any idea. Thank you
解决方案
推荐阅读
- ruby-on-rails - 如何更改每 n 次迭代的代码
- r - R Shiny 数据分析:我正在努力从我上传的 .csv 文件中渲染一个图
- powershell - 使用@Splat 发送邮件附件结果,执行成功后删除
- c# - 从 Xamarin Android 中的 TimePicker 获取选定的时间
- css - ReactJS 和渲染问题
- regex - htaccess - 如何比较环境变量 - 检查2个环境变量是否相同
- react-native - React Navigation 5:在 BottomTabNavigator 上实现自定义标题
- r - 在 R 中使用爪子——列出 AWS 工作区
- types - Rust:特征作为返回类型
- python - 在哪个数据库上可以将数据存储在子目录或多个文件夹中