Python 如何从列表字典的值创建元组列表?
字典Python 如何从列表字典的值创建元组列表?,python,list,dictionary,tuples,Python,List,Dictionary,Tuples,字典my_entities如下所示: {'Alec': [(1508, 1512), (2882, 2886), (3011, 3015), (3192, 3196), (3564, 3568), (6453, 6457)], 'Downworlders': [(55, 67)], 'Izzy': [(1499, 1503), (1823, 1827), (7455, 7459)], 'Ja
my_entities
如下所示:
{'Alec': [(1508, 1512),
(2882, 2886),
(3011, 3015),
(3192, 3196),
(3564, 3568),
(6453, 6457)],
'Downworlders': [(55, 67)],
'Izzy': [(1499, 1503), (1823, 1827), (7455, 7459)],
'Jace': [(1493, 1497),
(1566, 1570),
(3937, 3941),
(5246, 5250)]...}
[ (1508, 1512),
(2882, 2886),
(3011, 3015),
(3192, 3196),
(3564, 3568),
(6453, 6457),
(55, 67), (1499, 1503), (1823, 1827), (7455, 7459),...]
我希望能够将所有键的值保存在一个元组列表中,以便与其他列表进行比较
到目前为止,我已经尝试了以下代码:
from pprint import pprint
list_from_dict = []
for keys in my_entities:
list_from_dict = [].append(my_entities.values())
pprint(list_from_dict)
并且它输出None
我期望的输出如下所示:
{'Alec': [(1508, 1512),
(2882, 2886),
(3011, 3015),
(3192, 3196),
(3564, 3568),
(6453, 6457)],
'Downworlders': [(55, 67)],
'Izzy': [(1499, 1503), (1823, 1827), (7455, 7459)],
'Jace': [(1493, 1497),
(1566, 1570),
(3937, 3941),
(5246, 5250)]...}
[ (1508, 1512),
(2882, 2886),
(3011, 3015),
(3192, 3196),
(3564, 3568),
(6453, 6457),
(55, 67), (1499, 1503), (1823, 1827), (7455, 7459),...]
我如何调整代码来实现这一点
提前谢谢
编辑:
我没有找到其他问题的答案,因为它没有关键字字典
。如果它确实被视为一个副本,那么它可以被删除-我有我的答案。谢谢 使用或从itertools
模块:
from itertools import chain
d = {'Alec': [(1508, 1512),
(2882, 2886),
(3011, 3015),
(3192, 3196),
(3564, 3568),
(6453, 6457)],
'Downworlders': [(55, 67)],
'Izzy': [(1499, 1503), (1823, 1827), (7455, 7459)],
'Jace': [(1493, 1497),
(1566, 1570),
(3937, 3941),
(5246, 5250)]}
print(list(chain(*d.values())))
# [(1508, 1512), (2882, 2886), (3011, 3015), (3192, 3196), (3564, 3568),
# (6453, 6457), (55, 67), (1499, 1503), (1823, 1827), (7455, 7459),
# (1493, 1497), (1566, 1570), (3937, 3941), (5246, 5250)]
或:
这是对
my_entities.values()
的简单展平操作。所以[t代表我的实体中的v.values()代表我的实体中的t]`或者itertools.chain.from\u iterable(我的实体.values())
,如果您只需要一个迭代器。