python - 如何从元组列表中的字典中获取值?

标签 python dictionary

我有一个如下所示的字典,我想将表示 1、1 的值存储在列表中。

sc_dict=[('n', {'rh': 1}), ('n', {'rhe': 1}), ('nc', {'rhex': 1})]

我想要一个数组[1,1,1]

这是我的代码:

dict_part = [sc[1] for sc in sc_dict]

print(dict_part[1])

L1=[year for (title, year) in (sorted(dict_part.items(), key=lambda t: t[0]))]
print(L1)

最佳答案

>>> [v for t1, t2 in sc_dict for k, v in t2.items()]
[1, 1, 1]

t1t2 分别是每个元组的第一项和第二项,kv字典 t2 中的键值对。

关于python - 如何从元组列表中的字典中获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49861820/

相关文章:

python - 使用 PyParsing 匹配 2 个以上空格

javascript - Django 的 CSRF 验证失败,尽管 Firebug 说 cookies 选项卡下有一个 csrftoken。为什么?

python - 在 Python 中轻松地从/到 namespace /字典转储变量

c - 你知道支持 COW 事务的 C 字典吗?

java - 使用接口(interface)实例化和具体类实例化之间的区别? (HashMap、Map & List、ArrayList)

python - 类型错误 : 'int' object is unsubscriptable

python - Google App Engine - 非常慢且昂贵的备份和恢复?

PythonAnyWhere SECRET_KEY 设置不能为空

java - 嵌套 HashMap 无法正常工作

c# - 从 Linq 查询创建 Dictionary<int, List<int>>