单个字典包含同一键下的多个列表。希望将同一键下的所有值组合到一个列表中
我有一个单词,其中包含一个键下的多个列表 - 这是我的字典输出的一个示例:
print(dic)
输出:
{1: [[1, 2, 3, 4, 5, 6, 7],
[5, 1, 4, 34, 3, 65, 0, 2],
[2, 5, 4, 5, 7, 8, 4, 7]],
2: [[1, 5, 4, 5, 0, 1, 4, 6],
[2, 2, 3, 5, 6, 6, 3, 9]],
3: [[12, 35, 42, 53, 70, 71, 74, 76],
[6, 11, 16, 17, 38, 62, 66, 77]]}
我希望为每个键下的所有列表创建一个列表。 因此,我要寻找的内容的输出示例将是这样的:
{1: [[1, 2, 3, 4, 5, 6, 7, 5, 1, 4, 34, 3, 65, 0, 2, 2, 5, 4, 5, 7, 8, 4, 7]],
2: [[1, 5, 4, 5, 0, 1, 4, 6, 2, 2, 3, 5, 6, 6, 3, 9]],
3: [[12, 35, 42, 53, 70, 71, 74, 76, 6, 11, 16, 17, 38, 62, 66, 77]]}
我尝试了几乎没有成功的理解,以及我在dic.items()上迭代的循环。
I have a single dictionary that contains multiple lists under a single key -
here is an example of the output of my dictionary:
print(dic)
Output:
{1: [[1, 2, 3, 4, 5, 6, 7],
[5, 1, 4, 34, 3, 65, 0, 2],
[2, 5, 4, 5, 7, 8, 4, 7]],
2: [[1, 5, 4, 5, 0, 1, 4, 6],
[2, 2, 3, 5, 6, 6, 3, 9]],
3: [[12, 35, 42, 53, 70, 71, 74, 76],
[6, 11, 16, 17, 38, 62, 66, 77]]}
I am looking to create a single list for all lists under each key.
So an output example of what i am looking for would look like this:
{1: [[1, 2, 3, 4, 5, 6, 7, 5, 1, 4, 34, 3, 65, 0, 2, 2, 5, 4, 5, 7, 8, 4, 7]],
2: [[1, 5, 4, 5, 0, 1, 4, 6, 2, 2, 3, 5, 6, 6, 3, 9]],
3: [[12, 35, 42, 53, 70, 71, 74, 76, 6, 11, 16, 17, 38, 62, 66, 77]]}
I have tried a dict comprehension to little success as well as a for loop where i iterate over dic.items().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用字典理解:
此输出:
You can use a dictionary comprehension:
This outputs:
尝试使用
itertools.chain> itertools.chain.from_iterable
/code>/code> :Try with
itertools.chain.from_iterable
:使用
itertools.chain
:输出:
Using
itertools.chain
:Output: