Itertools.permutations returns <object> 而不是排列列表
Itertools.permutations returns <object> instead of list of permutations
当我输入:
import itertools
perm = itertools.permutations(List)
我得到:
<itertools.permutations object at 0x03042630>
而不是我的排列列表。谁能帮我得到包含所有排列的实际列表?
它returns 一个迭代器对象。如果要获取实际列表,可以使用 list
:
轻松地将此迭代器对象转换为列表
import itertools
l = [1, 2, 3]
perm = list(itertools.permutations(l))
给你
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
要遍历排列对象,您必须使用 for 循环:
import itertools
for permutation in itertools.permutations(L):
print permutation
当我输入:
import itertools
perm = itertools.permutations(List)
我得到:
<itertools.permutations object at 0x03042630>
而不是我的排列列表。谁能帮我得到包含所有排列的实际列表?
它returns 一个迭代器对象。如果要获取实际列表,可以使用 list
:
import itertools
l = [1, 2, 3]
perm = list(itertools.permutations(l))
给你
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
要遍历排列对象,您必须使用 for 循环:
import itertools
for permutation in itertools.permutations(L):
print permutation