python - 如何生成 3 个字符前缀的唯一列表
问题描述
如何在 python 3 中将顶部列表添加到以下输出:
['abc', 'def', 'ghi']
['adg', 'adh', 'adi', 'aeg', 'aeh', 'aei', 'afg', 'afh', 'afi', 'bdg', 'bdh', 'bdi', 'beg', 'beh', 'bei', 'bfg', 'bfh', 'bfi', 'cdg', 'cdh', 'cdi', 'ceg', 'ceh', 'cei', 'cfg', 'cfh', 'cfi']
如果可能的话,可以使用 for 循环来解决这个问题吗?
解决方案
您正在寻找itertools.product
:
from itertools import product
lst = ['abc', 'def', 'ghi']
print( [''.join(c) for c in product(*lst)] )
印刷:
['adg', 'adh', 'adi', 'aeg', 'aeh', 'aei', 'afg', 'afh', 'afi', 'bdg', 'bdh', 'bdi', 'beg', 'beh', 'bei', 'bfg', 'bfh', 'bfi', 'cdg', 'cdh', 'cdi', 'ceg', 'ceh', 'cei', 'cfg', 'cfh', 'cfi']
推荐阅读
- c++ - “无法打开 cygwin.S”以某个变量值显示
- unity3d - 如何绘制更大尺寸的标签?
- reactjs - Firebase,React:如何在不让令牌过期的情况下发出经过身份验证的 POST 请求?
- lstm - 我们可以创建一个具有共享 LSTM 层和嵌入层的 RNN 模型吗?
- php - 从 RecursiveDirectoryIterator 中排除目录和文件
- wordpress - 不同页面显示不同菜单
- swift - Firebase:如果孩子小于 queryLimited 中的指定数字,则无法检索孩子
- excel - 如何在当前单元格上使用 HYPERLINK 和 VLOOKUP?
- python - Jupyter Notebook中的Pyathena找不到aws配置文件
- reactjs - 更改活动索引时如何使React Native tabview滚动回选项卡顶部