python - 如何从给定的集合中找到子集,如下所示
问题描述
如果给定列表是 [1,2,3] 并且输出应该是 [[1], [2], [3], [1,2], [2,3], [1, 3],[1,2,3]]。那是如何获得所有可能的组合?
解决方案
import itertools
arr = [1, 2, 3]
res = []
for i in range(len(arr)):
combinations = itertools.combinations(arr, i + 1)
for c in combinations:
res.append(list(c))
print(res)
推荐阅读
- sql - Retrieving inner table records in Postgres when using MAX COUNT
- java - Need to serialize as JSON string using multiple separate keys in POJO using jackson?
- node.js - 我可以在不删除 package-lock.json 文件的情况下解决 npm 安装错误吗?
- java - JavaFX ComboBox 不呈现调整大小的单元格
- powerbi - 计算先前日期的 Powerbi DAX 公式
- python - 在django中查询多对多对象的属性
- postgresql - Google Cloud Function 访问 Cloud SQL PostgreSQL 数据库超时
- java - Sonarqube 错误地显示“可能的空指针取消引用”错误
- flutter - Flutter:setState() 不会触发构建
- angular - 角度 2 中的常量对象列表