在for循环声明中使用zip(* some_list)解压缩列表列表时,如何动态设置迭代器?

问题描述 投票:0回答:1

这是我的代码的基本设置方式,但是正如您看到的p1-p9是静态行为。如果我不知道将从zip(* P)中解开多少个项目怎么办?

#What if i dont know how many items are being unpacked with *P?
for p_1, p_2, ... p_9 in zip(*P):
    #doing something with p1-pn

我想问一下是否有一种方法可以动态设置我的迭代器,使其取决于zip(* P)的状态

for p_array in zip(*P):
    #Maybe there is some way to set up a dynamic list as an iterator?

python for-loop iterator zip iterable-unpacking
1个回答
0
投票

找到答案

for p in zip(*P):
   print(list(p))
© www.soinside.com 2019 - 2024. All rights reserved.