gpt4 book ai didi

python - 我如何编写一个函数,它使用尽可能多的 'for loops' 来使用带有字母字符串的列表来对带有空格的列表进行字谜排列?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:41:10 26 4
gpt4 key购买 nike

您好 stackoverflow 社区!几个星期以来,我一直在努力寻找一种方法来做到这一点而不寻求帮助(有点个人挑战),但我做不到,而且大学项目占用了我大部分时间,无法做到这一点令人沮丧我,因为它似乎不太难,但想不出怎么做。成为开发者的第一课就是学会与他人合作,对吧?所以我来这里寻求帮助。我有这段代码:

l = [None]*8
k = ['m','i','k','e']
for a in k:
for b in k:
for c in k:
l[0] = a
l[1] = b
l[2] = c
print(l)

输出这个:

['m', 'm', 'm', None, None, None, None, None]
['m', 'm', 'i', None, None, None, None, None]
['m', 'm', 'k', None, None, None, None, None]
['m', 'm', 'e', None, None, None, None, None]
['m', 'i', 'm', None, None, None, None, None]
['m', 'i', 'i', None, None, None, None, None]
['m', 'i', 'k', None, None, None, None, None]
['m', 'i', 'e', None, None, None, None, None]
['m', 'k', 'm', None, None, None, None, None]
['m', 'k', 'i', None, None, None, None, None]
['m', 'k', 'k', None, None, None, None, None]
['m', 'k', 'e', None, None, None, None, None]
['m', 'e', 'm', None, None, None, None, None]
['m', 'e', 'i', None, None, None, None, None]
['m', 'e', 'k', None, None, None, None, None]
['m', 'e', 'e', None, None, None, None, None]
['i', 'm', 'm', None, None, None, None, None]
['i', 'm', 'i', None, None, None, None, None]
['i', 'm', 'k', None, None, None, None, None]
['i', 'm', 'e', None, None, None, None, None]
['i', 'i', 'm', None, None, None, None, None]
['i', 'i', 'i', None, None, None, None, None]
['i', 'i', 'k', None, None, None, None, None]
['i', 'i', 'e', None, None, None, None, None]
['i', 'k', 'm', None, None, None, None, None]
['i', 'k', 'i', None, None, None, None, None]
['i', 'k', 'k', None, None, None, None, None]
['i', 'k', 'e', None, None, None, None, None]
['i', 'e', 'm', None, None, None, None, None]
['i', 'e', 'i', None, None, None, None, None]
['i', 'e', 'k', None, None, None, None, None]
['i', 'e', 'e', None, None, None, None, None]
['k', 'm', 'm', None, None, None, None, None]
['k', 'm', 'i', None, None, None, None, None]
['k', 'm', 'k', None, None, None, None, None]
['k', 'm', 'e', None, None, None, None, None]
['k', 'i', 'm', None, None, None, None, None]
['k', 'i', 'i', None, None, None, None, None]
['k', 'i', 'k', None, None, None, None, None]
['k', 'k', 'm', None, None, None, None, None]
['k', 'k', 'i', None, None, None, None, None]
['k', 'k', 'k', None, None, None, None, None]
['k', 'k', 'e', None, None, None, None, None]
['k', 'e', 'm', None, None, None, None, None]
['k', 'e', 'i', None, None, None, None, None]
['k', 'e', 'k', None, None, None, None, None]
['k', 'e', 'e', None, None, None, None, None]
['e', 'm', 'm', None, None, None, None, None]
['e', 'm', 'i', None, None, None, None, None]
['e', 'm', 'k', None, None, None, None, None]
['e', 'm', 'e', None, None, None, None, None]
['e', 'i', 'm', None, None, None, None, None]
['e', 'i', 'i', None, None, None, None, None]
['e', 'i', 'k', None, None, None, None, None]
['e', 'i', 'e', None, None, None, None, None]
['e', 'k', 'm', None, None, None, None, None]
['e', 'k', 'i', None, None, None, None, None]
['e', 'k', 'k', None, None, None, None, None]
['e', 'k', 'e', None, None, None, None, None]
['e', 'e', 'm', None, None, None, None, None]
['e', 'e', 'i', None, None, None, None, None]
['e', 'e', 'k', None, None, None, None, None]
['e', 'e', 'e', None, None, None, None, None]

我想做的是有一个函数,可以对我想要的 nonelist 中的空格数执行此操作。就像传递一个参数来选择我想要的 None 数来代替。我想象着这个函数是如何工作的,在我看来它有 3 个参数(None 列表的大小,要替换的字母列表,要替换的 None 的数量)。我熟悉递归,但想不出使用它来实现它的方法。提前致谢:)

最佳答案

递归是要走的路:

def print_permutations(k, l, level = 0):
if level == len(l):
print(l)
else:
for a in k:
l[level] = a
print_permutations(k, l, level+1 )



l = [None]*8
k = ['m','i','k','e']

print_permutations(k, l)

或者您可以使用标准工具:

from itertools import combinations_with_replacement


k = ['m','i','k','e']

for p in combinations_with_replacement(k, 8):
print(p)
# print(list(p)) if you want to print it as a list

顺便说一句。 k='mike' 的效果相同。

关于python - 我如何编写一个函数,它使用尽可能多的 'for loops' 来使用带有字母字符串的列表来对带有空格的列表进行字谜排列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41770444/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com