new_list=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
all_possible=[] #print of all possible combinations
y=0
z=0
def writer():
for z in range(0, 26):
all_possible.append(new_list[0] + "" + new_list[y] + "" + new_list[z])
z = 0
def reader():
for y in range(0, 26):
writer()
reader()
print(all_possible)
all_possible=[]
for y in range(0, 26):
for z in range(0, 26):
all_possible.append(new_list[0] + "" + new_list[y] + "" +new_list[z])
z = 0
print(all_possible)
所以我创建了这段代码以打印一个列表,该列表打印出所有包含3个字符的字母组合,我也想使用一个函数来执行此操作,但是由于某种原因,当我将相同的内容放入函数中时,它并没有由于某些原因,以相同的方式打印。你能帮忙吗?
答案 0 :(得分:0)
您的函数应接受参数,以便您可以传递列表引用以修改函数:
new_list=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
all_possible=[] #print of all possible combinations
def writer(l, y):
for z in range(0, 26):
l.append(new_list[0] + "" + new_list[y] + "" + new_list[z])
def reader(l):
for y in range(0, 26):
writer(l, y)
reader(all_possible)
print(all_possible)
答案 1 :(得分:0)
如果您希望两个列表都提供相同的输出,则应向函数编写器添加一个参数。否则,y的值将始终为零,并且永远不变。
new_list=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
all_possible1=[] #print of all possible combinations
y=0
z=0
def writer(y):
for z in range(0, 26):
all_possible1.append(new_list[0] + "" + new_list[y] + "" + new_list[z])
z = 0
def reader():
for y in range(0, 26):
writer(y)
reader()
print(all_possible1)
all_possible2=[]
for y in range(0, 26):
for z in range(0, 26):
all_possible2.append(new_list[0] + "" + new_list[y] + "" +new_list[z])
z = 0
print(all_possible2)
print(all_possible2 == all_possible1)
答案 2 :(得分:0)
如果您想轻松做到
import itertools
list(itertools.combinations(new_list, 3))
但是根据您发布的代码,我认为您将希望按照自己的方式做,并了解有关python语法的更多信息,因此此答案仅是一个建议