在Python中动态创建循环并存储值
在Python中,是否可以动态地创建循环并存储一些值呢?
比如说,创建一个两层循环:
for word1 in word_list:
for word2 in word_list:
final_word=word1+word2
再比如,创建一个三层循环:
for word1 in word_list:
for word2 in word_list:
for word3 in word_list:
final_word=word1+word2+word3
2 个回答
0
import time
startedsomething = 0
finishingoff = 1
roundtwo = 2
roundone = 1
def repeat():
def one():
if roundone < roundtwo:
print("this is round ONE 1111111 coming through")
one()
time.sleep(3)
def two():
global roundone, roundtwo
if roundone == roundtwo:
print("this is round TWO 2222222 coming through if finishingoff is set to 10 that would be 9 rounds and this code should show up 4 times")
roundtwo += 1
roundone -= 1
roundone += 1
time.sleep(3)
two()
print("This is some interesting code I hope you like this loop")
print("Whats really happening is that First round one gets picked up than this code, than round two, than this code than round one than this code round one can referr to something in round two especially if you have a general variable on top and a global variable the same in round two and round two can refer to round one")
time.sleep(3)
def startingsomething():
global startedsomething
if finishingoff > startedsomething:
print("Now you happining look at you you started something, lets make sure, you put everything here you want python to remember until your finishing off")
time.sleep(3)
startedsomething += 1
startingsomething()
def finalwords():
global finishingoff
if finishingoff == startedsomething:
print("you've come to the end of your loop")
time.sleep(3)
finishingoff += 1
finalwords()
while finishingoff < 10:
repeat()
当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的方式解释清楚。
0
你是不是想从这个列表中获取所有的n组合单词?比如说,如果列表是 ['a','b','c']
,你是不是只想要 ['aaa','aab','aac','aba','abb','abc','aca',等等...]
这些组合?如果是这样的话,你只需要对这个组合进行映射和连接,重复的次数就是你想要连接的单词数量:
map(''.join, itertools.product(wordlist, repeat=n))