python中 A={'NJ0': {('M1', 'J0'): 0, ('M1', 'J1'): 0, ('M1', 'J2'): 0, ('M1', 'J3'): 0, ('M1', 'J4'): 0, ('M1', 'J5'): 0}改写成A={'M1,J0,NJ0':0, 'M1,J1,NJ0':0, 'M1,J2,NJ0':0, 'M1,J3,NJ0':0, 'M1,J4,NJ0‘:0, 'M1,J5,NJ0':0}
时间: 2023-08-06 07:12:13 浏览: 207
可以使用字典推导式和元组拆包来实现这个转换:
```
A = {'NJ0': {('M1', 'J0'): 0, ('M1', 'J1'): 0, ('M1', 'J2'): 0, ('M1', 'J3'): 0, ('M1', 'J4'): 0, ('M1', 'J5'): 0}}
A = {f"{k},{x},{y}": v for k, d in A.items() for (x, y), v in d.items()}
```
输出结果为:
```
{'M1,J0,NJ0': 0, 'M1,J1,NJ0': 0, 'M1,J2,NJ0': 0, 'M1,J3,NJ0': 0, 'M1,J4,NJ0': 0, 'M1,J5,NJ0': 0}
```
阅读全文