用于反转 for 循环执行顺序的迭代器?
Iterator to reverse a for loop's order of execution?
我已经在文档中搜索 itertools 以及 SO 一段时间了,但还没有找到我要找的东西。我希望反转 for 循环的执行顺序,以便发生以下情况:
for letter in ["A","B","C"]:
do_something(letter)
do_something_else(letter)
def do_something(letter):
print("do_something with " + letter)
def do_something_else(letter):
print("do something_else with " + letter)
for 循环的正常输出为:
do_something with A
do something_else with A
do_something with B
do something_else with B
do_something with C
do something_else with C
我希望它(迭代器)输出如下:
do_something with A
do_something with B
do_something with C
do something_else with A
do something_else with B
do something_else with C
我不会说谎这是漫长的一天所以答案可能就在我眼皮底下,到目前为止我想出的最好的答案是:
for letter in ["A","B","C"]:
do_something(letter)
for letter in ["A","B","C"]:
do_something_else(letter)
def do_something(letter):
print("do_something with " + letter)
def do_something_else(letter):
print("do something_else with " + letter)
但是因为我在重复 for 循环,所以感觉很笨拙。非常感谢任何帮助。谢谢大家!
进行两次迭代:
for letter in ["A","B","C"]:
do_something(letter)
for letter in ["A","B","C"]:
do_something_else(letter)
另一种选择是对函数进行外循环,对字母进行内循环。
for f in [do_something, do_something_else]:
for letter in ["A","B","C"]:
f(letter)
然而,另一种选择是在输入函数上使用 itertools/product(输出根据需要)。
from itertools import product
for f, letter in product([do_something, do_something_else], ["A","B","C"]):
f(letter)
怎么样
results = [list(map(func,letters)) for func in [do_something,do_something_else]]
您可以使用 itertools.tee()
function to make two independent letter iterators and the built-in zip()
函数将函数和字母“编织”在一起,如下所示:
from itertools import tee
def do_something(letter):
print("do_something with " + letter)
def do_something_else(letter):
print("do something_else with " + letter)
for function, letters in zip((do_something, do_something_else), tee(["A","B","C"]) ):
for letter in letters:
function(letter)
我已经在文档中搜索 itertools 以及 SO 一段时间了,但还没有找到我要找的东西。我希望反转 for 循环的执行顺序,以便发生以下情况:
for letter in ["A","B","C"]:
do_something(letter)
do_something_else(letter)
def do_something(letter):
print("do_something with " + letter)
def do_something_else(letter):
print("do something_else with " + letter)
for 循环的正常输出为:
do_something with A
do something_else with A
do_something with B
do something_else with B
do_something with C
do something_else with C
我希望它(迭代器)输出如下:
do_something with A
do_something with B
do_something with C
do something_else with A
do something_else with B
do something_else with C
我不会说谎这是漫长的一天所以答案可能就在我眼皮底下,到目前为止我想出的最好的答案是:
for letter in ["A","B","C"]:
do_something(letter)
for letter in ["A","B","C"]:
do_something_else(letter)
def do_something(letter):
print("do_something with " + letter)
def do_something_else(letter):
print("do something_else with " + letter)
但是因为我在重复 for 循环,所以感觉很笨拙。非常感谢任何帮助。谢谢大家!
进行两次迭代:
for letter in ["A","B","C"]:
do_something(letter)
for letter in ["A","B","C"]:
do_something_else(letter)
另一种选择是对函数进行外循环,对字母进行内循环。
for f in [do_something, do_something_else]:
for letter in ["A","B","C"]:
f(letter)
然而,另一种选择是在输入函数上使用 itertools/product(输出根据需要)。
from itertools import product
for f, letter in product([do_something, do_something_else], ["A","B","C"]):
f(letter)
怎么样
results = [list(map(func,letters)) for func in [do_something,do_something_else]]
您可以使用 itertools.tee()
function to make two independent letter iterators and the built-in zip()
函数将函数和字母“编织”在一起,如下所示:
from itertools import tee
def do_something(letter):
print("do_something with " + letter)
def do_something_else(letter):
print("do something_else with " + letter)
for function, letters in zip((do_something, do_something_else), tee(["A","B","C"]) ):
for letter in letters:
function(letter)