gpt4 book ai didi

python嵌套字典循环

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:46 25 4
gpt4 key购买 nike

是否有更好/更清洁/更短的方法来获得与以下相同的输出?

import plistlib
pl = plistlib.readPlist('/Users/username/Documents/wifi1.plist')

n = len(pl)
count = 0
while (count < n):
print('----------------')
print(pl[count]['NOISE'])
print(pl[count]['RSSI'])
print(pl[count]['SSID_STR'])
print(pl[count]['BSSID'])
count += 1

我试过:

for sub_dict in pl.values():
print(sub_dict['NOISE'], sub_dict['RSSI'], sub_dict['SSID_STR'], sub_dict['BSSID'])

但我得到:

Traceback (most recent call last):
File "plistread.py", line 17, in <module>
for sub_dict in pl.values():
AttributeError: 'list' object has no attribute 'values'

最佳答案

你只需要:

for sub_dict in pl:

因为 pl 是一个列表,遍历该列表将依次为您提供每个子字典。

一个简单的例子:

>>> l = [1,2,3,4]
>>> for x in l:
... print x,
...
1 2 3 4

关于python嵌套字典循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8892686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com