cuốn sách gpt4 ai đã làm

Python Itertools 排列

In lại Tác giả: Walker 123 更新时间:2023-11-28 22:53:31 29 4
mua khóa gpt4 Nike

我目前正在编写一个使用 itertools 的程序,其中的一部分似乎无法正常运行。我希望确定排列函数输出列表长度的输入等于它生成输出的列表长度。换句话说,我有

import itertools

b = 0
c = 9
d = [0,1,2]
e = len(d)


while b < c:
d.append(b)
b = b+1

print([x for x in itertools.permutations(d,e)])

我希望生成所有可能的 d 等于此长度的排列。我一直在试验这个,似乎第二个确定者必须是一个整数。我什至尝试创建一个新变量 f,并让 f = int(e) 然后在 print 语句中用 f 替换 e,但没有成功。使用其中任何一个,我得到的都是 [()]

Cảm ơn sự giúp đỡ của bạn.

câu trả lời hay nhất

您需要在构建列表sau đócài đặte. len(d) 返回一个值,而不是对列表长度的引用。

d = range(0,9) # build an arbitrary list here
# this creates a list of numbers: [0,1,2,3,4,5,6,7,8]
e = len(d)
print list(itertools.permutations(d, e))

请注意,排列的数量非常多,因此将所有排列存储在列表中会消耗大量内存 - 你最好这样做:

d = range(0,9)
e = len(d)
for p in itertools.permutations(d, e):
print p

关于Python Itertools 排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19371437/

29 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress