我在尝试使用递归制作排列代码时遇到了麻烦。这假设将一个列表返回给使用,其中包含每个字母的所有可能位置。所以对于这个词cat
,它应该是 return ['cat','act',atc,'cta','tca','tac']
。到目前为止我有这个
def permutations(s):
lst=[]
if len(s) == 1 or len(s) == 0 :
# Return a list containing the string, not the string
return [s]
# Call permutations to get the permutations that don't include the
# first character of s
plst = permutations(s[1:])
print(plst)
for item in plst:
print (item)
plst= permutations(s[1+1:])
# Now move through each possible position of the first character
# and create a new string that puts that character into the strings
# in plst
for i in range(len(s)):
pass
# Create a new string out of item
# and put it into lst
# Modify
for item in lst:
print(index)
那里有步骤,但我不确定如何使用它们