Python中文
首页
教程
问答
标签
搜索
登录
注册
Python itertools对多个数组的permutations()用法
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我正在用Python构建<strike>字典</strike>列表,目的是逐个遍历每个字典并连接所有不同的组合。在<strike>字典中,每个“产品”有三个不同的组成部分:</p> <p>1)字母,例如<code>'A'</code>(产品代码的第一部分,对每个产品条目都是唯一的)。假设范围是:</p> <pre><code>['A', 'B', 'C'] </code></pre> <p>2)字母和数字,例如<code>'S2'</code>(第二部分有几个变体…可能是<code>'D3'</code>或<code>'E5'</code>)</p> <p>3)一个句点(<code>'.'</code>)和一个字母,例如<code>'.X'</code>(第三部分,每个产品条目都是唯一的)。假设范围是:</p> <pre><code>['.X', '.Y', '.Z'] </code></pre> <p>由于上面列出的第二部分有最多的变化,我的初始假设是将第一部分和第三部分一起构建<strike>dicts</strike>列表,以减少不同列表的数量<strike>dicts</strike>,因为它们是唯一成对的,例如<code>A.Z'</code>…但是,我仍然需要拆分每个条目,然后通过一些“连接”命令在它们之间插入第二部分。所以,问题是:如果我有另一个包含第二部分所有变体的<strike>dict</strike>列表,我应该使用什么函数来构建产品的所有变体?你知道吗</p> <p>示例的总组合:</p> <pre><code>ListOne = ['A', 'B', 'C'] ListTwo = ['D3', 'D4', 'D5', 'E3', 'E4', 'E5'] ListThr = ['.X', '.Y', '.Z'] </code></pre> <p>我需要创建新的<strike>dicts</strike>列表作为所有三个<strike>dicts</strike>列表的串联,例如<code>'AD3.X'</code>,但是<code>ListOne</code>和<code>ListThr</code>没有变体,它总是<code>'A'</code>与<code>'.X'</code>或<code>'B'</code>匹配,并且<code>'C'</code>与<code>'.Y'</code>匹配…<code>ListTwo</code>在<code>ListOne</code>和<code>ListThr</code>产品之间串联的产品将需要迭代,因此所有可能的组合都作为一个新的<strike>dict</strike>列表输出,例如</p> <pre><code>ListOneNew = ['AD3.X', 'AD4.X', AD5.X', 'AE3.X', 'AE4.X', 'AE5.X'] ListTwoNew = ['BD3.Y', 'BD4.Y', 'BD5.Y', <and so on...>] </code></pre> <p>为了简单起见,脚本应该有<code>ListOne</code>和<code>ListThr</code>的合并版本吗</p> <pre><code>List = ['A.X', 'B.X', 'C.Z'] </code></pre> <p>然后用<code>ListTwo</code>产品拆分并连接,或者只使用三个<code>List</code>产品并从那里连接?你知道吗</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<pre><code>from itertools import product result = [a[0] + a[1] + a[2] for a in list(product(DictOne, DictTwo, DictThr))] </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何在乒乓球比赛中预测球的轨迹,对于AI球拍预测?
8 回答
如何在乒乓球游戏中阻止球
9 回答
如何在乘法和模中不乘空间?
1 回答
如何在乘法和除以2个不同的数字之间进行交换?
2 回答
如何在也是数据一部分的单个字符上拆分大字符串
9 回答
如何在乾草堆中找到針,有更好的解決方案嗎?
4 回答
如何在事件wxWidgets中传递自定义数据
2 回答
如何在事件中使用lambda i=i?
6 回答
如何在事件中心只接收最近的数据
4 回答
如何在事件发生之前保持云函数运行?
1 回答
如何在事件发生后使页面重定向到同一页面
10 回答
如何在事件回调之间保持python生成器的状态
1 回答
如何在事件处理程序(pythonsocket、sphinx)中保留docstring
7 回答
如何在事件处理程序中更改wxRichTextCtrl的光标位置?
6 回答
如何在事件处理程序中访问外部对象?
8 回答
如何在事件循环中将协程打包为正常函数?
4 回答
如何在事件循环之外运行协同程序?
10 回答
如何在事件循环结束时为并发未来的所有线程调用类方法?
6 回答
如何在事件文件中只保留一份摘要?
8 回答
如何在事件模板中添加事件
9 回答