python - 在字典中查找所有可能性(Python 2.7)

标签 python python-2.7

<分区>

我在 python 中有以下字典:

{main1: {x: 1, y: 2}, main2: {a: 1, b: 2}}

我需要的结果是所有可能的组合,比如:

{main1: {x: 1}}
{main1: {y: 2}}
{main1: {x: 1, y:2}}
{main2: {a: 1}}
{main1: {x: 1}, main2: {a: 1}}
{main1: {y: 2}, main2: {a: 1}}
{main1: {x: 1, y:2}, main2: {a: 1}}
...

等我觉得有一些 pythonic 解决方案,但我找不到。有什么想法吗?

最佳答案

itertools.combinations()

或许能帮到你。 您可以调用它,在基本循环中获取大小 1、2 等的组合。

关于python - 在字典中查找所有可能性(Python 2.7),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31437941/

相关文章:

python-2.7 - 确定Unix域套接字上传入请求的docker容器

python - 导入错误 : no module named html. 解析器

Python Win32Com - ExportAsFixedFormat - MS Excel 2010 文件

python - 将所有 next_elements 包装在 BeautifulSoup 中

python - Django Localhost CORS 不工作

OS X Yosemite 上的 Python 解释器——使用哪一个?

python - global 关键字内联全局是否与 python 中的局部变量声明完全相同?

python - from django.db import utils ImportError 无法导入名称 utils?

python - wxPython 处理 SIGTERM/SIGINT

python - 为自动更正程序快速保存和检索 python 数据结构?