小编Art*_*oev的帖子

通过字典中的字典进行一行迭代

G2 = {'a': {'c': 1, 'b': 1}, 'b': {'a': 1, 'c': 1}}

b = G2.values()

for i in b:
    for key, value in i.items():
        list.append(key)

#result: ['c', 'b', 'a', 'c']
Run Code Online (Sandbox Code Playgroud)

我可以得到相同的结果但使用列表生成器吗?我是这样试的:

list2 = [key for key, value in i.items() for i in b]

#but i get: ['a', 'a', 'c', 'c']
Run Code Online (Sandbox Code Playgroud)

python dictionary python-3.x

2
推荐指数
1
解决办法
1863
查看次数

标签 统计

dictionary ×1

python ×1

python-3.x ×1