【发布时间】:2021-07-24 17:38:52
【问题描述】:
我应该如何通过字典推导获得所需的输出?
{'R': {0: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
1: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
2: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
3: {2: 0, 3: 0, 4: 0, 5: 0, 1: 0}},
'L': {0: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
1: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
2: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
3: {2: 0, 3: 0, 4: 0, 5: 0, 1: 0}},
'B': {0: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
1: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
2: {1: 0, 2: 0, 3: 0, 4: 0, 5: 0},
3: {2: 0, 3: 0, 4: 0, 5: 0, 1: 0}}}
我通过下面的代码得到了它:
d_clas = {'B':{} , 'C':{}, 'D':{}}
l_uniq = [array([1, 2, 3, 4, 5], dtype=int64),
array([1, 2, 3, 4, 5], dtype=int64),
array([1, 2, 3, 4, 5], dtype=int64),
array([2, 3, 4, 5, 1], dtype=int64)]
for i in d_clas:
c_clas = {}
for j in range(len(l_uniq)-1):
c_clas[j] = {}
for k in l_uniq[j]:
c_clas[j][k] = 0
d_clas[i] = c_clas
【问题讨论】:
-
请查看简介tour,并查看how to ask a good question 以了解社区的期望并帮助您改进当前和未来的问题,从而帮助您获得更好的答案。请查看Stack Overflow question checklist。
标签: python dictionary dictionary-comprehension