You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python:如何将permutations生成的元组转为子列表

解决permutations输出元组转子列表的问题

itertools.permutations 默认返回元组类型的排列结果,要得到子列表格式,有两种简单的处理方式:

方法一:列表推导式

遍历每个排列结果,用 list() 把元组转换为列表:

import itertools
G3 = [[0, 4, 5]]
A = [list(p) for p in itertools.permutations(G3[0], 2)]
print(A)

方法二:使用map函数

用 map() 批量将元组转为列表,再转为最终的列表:

import itertools
G3 = [[0, 4, 5]]
A = list(map(list, itertools.permutations(G3[0], 2)))
print(A)

两种方法运行后都会输出期望的结果:

[[0, 4], [0, 5], [4, 0], [4, 5], [5, 0], [5, 4]]

内容的提问来源于stack exchange,提问作者rajunarlikar123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 01:47:04