首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python :如何从使用元组索引的嵌套字典创建平面字典?

Python :如何从使用元组索引的嵌套字典创建平面字典?
EN

Stack Overflow用户
提问于 2020-06-27 03:02:00
回答 3查看 60关注 0票数 0

我有一个嵌套的字典,如下所示:

代码语言:javascript
复制
{(1, 1): {'Transportation_Cost': 4},
 (1, 2): {'Transportation_Cost': 5},
 (1, 3): {'Transportation_Cost': 6},
 (1, 4): {'Transportation_Cost': 8},
 (1, 5): {'Transportation_Cost': 10},
 (2, 1): {'Transportation_Cost': 6},
 (2, 2): {'Transportation_Cost': 4},
 (2, 3): {'Transportation_Cost': 3},
 (2, 4): {'Transportation_Cost': 5},
 (2, 5): {'Transportation_Cost': 8},
 (3, 1): {'Transportation_Cost': 9},
 (3, 2): {'Transportation_Cost': 7},
 (3, 3): {'Transportation_Cost': 4},
 (3, 4): {'Transportation_Cost': 2},
 (3, 5): {'Transportation_Cost': 4}}

我想把它转换成一个平面字典,看起来像这样:

代码语言:javascript
复制
{(1, 1): 4,
 (1, 2): 5,
 (1, 3): 6,
 (1, 4): 8,
 (1, 5): 10,
 (2, 1): 6,
 (2, 2): 4,
 (2, 3): 3,
 (2, 4): 5,
 (2, 5): 8,
 (3, 1): 9,
 (3, 2): 7,
 (3, 3): 4,
 (3, 4): 2,
 (3, 5): 4}

我希望有一个简单的函数,可以扁平化我的嵌套字典。我尝试了here中的FlatDict函数,但我不确定如何修改它,因为我的字典中有一个元组。

对于如何获得所需的输出有什么建议吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-06-27 03:09:06

您可以尝试使用 dict Comprehension

代码语言:javascript
复制
dct={(1, 1): {'Transportation_Cost': 4},
 (1, 2): {'Transportation_Cost': 5},
 (1, 3): {'Transportation_Cost': 6},
 (1, 4): {'Transportation_Cost': 8},
 (1, 5): {'Transportation_Cost': 10},
 (2, 1): {'Transportation_Cost': 6},
 (2, 2): {'Transportation_Cost': 4},
 (2, 3): {'Transportation_Cost': 3},
 (2, 4): {'Transportation_Cost': 5},
 (2, 5): {'Transportation_Cost': 8},
 (3, 1): {'Transportation_Cost': 9},
 (3, 2): {'Transportation_Cost': 7},
 (3, 3): {'Transportation_Cost': 4},
 (3, 4): {'Transportation_Cost': 2},
 (3, 5): {'Transportation_Cost': 4}}

dct={k:v['Transportation_Cost'] for k,v in dct.items()}
print(dct)

输出:

代码语言:javascript
复制
{(1, 1): 4,
 (1, 2): 5,
 (1, 3): 6,
 (1, 4): 8,
 (1, 5): 10,
 (2, 1): 6,
 (2, 2): 4,
 (2, 3): 3,
 (2, 4): 5,
 (2, 5): 8,
 (3, 1): 9,
 (3, 2): 7,
 (3, 3): 4,
 (3, 4): 2,
 (3, 5): 4}
票数 3
EN

Stack Overflow用户

发布于 2020-06-27 03:42:13

下面是如何使用dict的理解:

代码语言:javascript
复制
d = {(1, 1): {'Transportation_Cost': 4},
     (1, 2): {'Transportation_Cost': 5},
     (1, 3): {'Transportation_Cost': 6},
     (1, 4): {'Transportation_Cost': 8},
     (1, 5): {'Transportation_Cost': 10},
     (2, 1): {'Transportation_Cost': 6},
     (2, 2): {'Transportation_Cost': 4},
     (2, 3): {'Transportation_Cost': 3},
     (2, 4): {'Transportation_Cost': 5},
     (2, 5): {'Transportation_Cost': 8},
     (3, 1): {'Transportation_Cost': 9},
     (3, 2): {'Transportation_Cost': 7},
     (3, 3): {'Transportation_Cost': 4},
     (3, 4): {'Transportation_Cost': 2},
     (3, 5): {'Transportation_Cost': 4}}

d = {k:d[k]['Transportation_Cost'] for k in d}

print(d)

输出:

代码语言:javascript
复制
{(1, 1): 4,
 (1, 2): 5,
 (1, 3): 6,
 (1, 4): 8,
 (1, 5): 10,
 (2, 1): 6,
 (2, 2): 4,
 (2, 3): 3,
 (2, 4): 5,
 (2, 5): 8,
 (3, 1): 9,
 (3, 2): 7,
 (3, 3): 4,
 (3, 4): 2,
 (3, 5): 4}
票数 1
EN

Stack Overflow用户

发布于 2020-06-27 03:13:59

您可以从Series对象创建字典,而不是编辑字典。

代码语言:javascript
复制
pd.Series(tc).to_dict()

这样你就不必担心字典格式的字典了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62601276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档