当我知道有 48 个事实时,for 循环仅打印第一个实例

问题描述 投票:0回答:1
import json

f = open('messages_1.json')
data = json.load(f)
d = 'call_duration'
calls = None

for i in data['messages']:
    if d in i:
        calls = i
        break
print(calls)

我试图获取呼叫日志,但代码仅返回字典列表中的第一个,如果我删除break语句,它会返回一个不同的实例,但同样,只返回1。

python json dictionary for-loop
1个回答
0
投票

你可以使用类似的东西:

import json

f = open('messages_1.json')
data = json.load(f)
calls = [i for i in data['messages'] if 'call_duration' in i]
print(calls)
© www.soinside.com 2019 - 2024. All rights reserved.