gpt4 book ai didi

python - Python 中的嵌套 for 循环

转载 作者:太空狗 更新时间:2023-10-29 21:25:02 26 4
gpt4 key购买 nike

我想做类似的事情

for a in [0..1]:
for b in [0..1]:
for c in [0..1]:
do something

但是,我可能有 15 个不同的变量。有没有更简单的方法,比如

for a, b, c in [0..1]:
do something

感谢您的帮助

最佳答案

itertools.product :

import itertools
for a,b,c in itertools.product([0, 1], repeat=3):
# do something

关于python - Python 中的嵌套 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7164162/

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