【发布时间】:2019-04-30 12:56:40
【问题描述】:
我有以下功能:
def count_chars(e):
return len(e)
我正在迭代一个json如下:
在:
a_lis = []
with open('../JSON_FILE.json','r') as fa:
a = json.load(fa)
for e in a['entries']:
pprint(e)
输出:
{'data': ['string'], 'type': 'one'}
{'data': ['a string '], 'type': 'one'}
{'data': ['another string'], 'type': 'three'}
...
{'data': ['one more string'], 'type': 'two'}
如何应用count_chars 函数并将其添加或更新为'data' 列表中的新字符串?例如,预期的输出如下所示:
{'data': ['string','6'], 'type': 'one'}
{'data': ['a string','8'], 'type': 'one'}
{'data': ['another string','14'], 'type': 'three'}
...
{'data': ['one more string','15'], 'type': 'two'}
更新:
我发现我的列表中有不止一项,例如:['first', 'second string']?我怎样才能返回['first', len_1, 'second string', len_2]
【问题讨论】:
标签: python json python-3.x loops dictionary