单个字典包含同一键下的多个列表。希望将同一键下的所有值组合到一个列表中

发布于 2025-02-11 00:11:16 字数 664 浏览 1 评论 0原文

我有一个单词,其中包含一个键下的多个列表 - 这是我的字典输出的一个示例:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情仇皆在手 2025-02-18 00:11:16

您可以使用字典理解:

{key: [[item for sublist in value for item in sublist]] for key, value in data.items()}

此输出:

{
 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]]
}

You can use a dictionary comprehension:

{key: [[item for sublist in value for item in sublist]] for key, value in data.items()}

This outputs:

{
 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 2025-02-18 00:11:16

尝试使用 itertools.chain> itertools.chain.from_iterable/code>/code>

>>> import itertools

>>> {k : [list(itertools.chain.from_iterable(v))] for k,v in dct.items()}
{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]]}

Try with itertools.chain.from_iterable:

>>> import itertools

>>> {k : [list(itertools.chain.from_iterable(v))] for k,v in dct.items()}
{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]]}
落叶缤纷 2025-02-18 00:11:16

使用 itertools.chain

from itertools import chain

out = {k: [list(chain.from_iterable(l))]
       for k,l in dic.items()}

输出:

{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]]}

Using itertools.chain:

from itertools import chain

out = {k: [list(chain.from_iterable(l))]
       for k,l in dic.items()}

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]]}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文