Python 我需要找到列表的所有排列

Python 我需要找到列表的所有排列,python,Python,我需要找到一个用户输入的4位数字的所有排列 我已经尝试过使用itertools.permutation,但它不起作用,我只能使用预设列表使代码工作 import itertools NumInput = str(input('Type here: ')) magicList = list(NumInput) itertools.permutations(magicList) print(magicList) 我希望itertools.permutation可以打印所有可能的perm,但它没有。

我需要找到一个用户输入的4位数字的所有排列

我已经尝试过使用itertools.permutation,但它不起作用,我只能使用预设列表使代码工作

import itertools
NumInput = str(input('Type here: '))
magicList = list(NumInput)
itertools.permutations(magicList)
print(magicList)

我希望itertools.permutation可以打印所有可能的perm,但它没有。我需要代码来打印输入的4位数字的所有可能组合,但我不知道您的代码是如何接近正确的:
itertools.permutations
是一个返回迭代器的函数,但不打印任何内容。因此,获取输出并打印它是您的工作,如下所示:

# get the itertools.combinations iterator
perms = itertools.permutations(magicList) 
# convert to a list
perms = list(perms)
# print it
print(perms) 
结果:

[('1', '2', '3', '4'), ('1', '2', '4', '3'), ...

您从未分配过
itertools.permutations(magicList)
的结果!!