如何构建一个同时识别两个不同列表的for循环



我得到了一个档案列表和一个numpy np。数组中,列表中的文件在numpy np中具有相同的对应关系。数组"list">

我想知道如何在列表项中迭代一个for循环,如:

for i in range (list):
get_items = (i, f) #when i is the first element in the list and f is the same in the numpy np.array
到目前为止,我所做的是一个for循环,在列表 中迭代
for i in range(list):
get_items = the_class_i_need(i, _)

"_"这就是我困惑的地方,因为我调用了numpy np。array。我知道它不起作用,因为我没有单独调用元素本身,而是调用里面的200个元素。不知何故,我认为我应该在for循环中做一个for循环,但我自己学会了这些,这对我来说很难。所以谢谢你的帮助!!

如上所述,可以使用zip()函数并行遍历列表和np数组,方法如下:

for i, f in zip(list_archives, np_archives):
get_items = the_class_i_need(i, f)

例如:

list_archives = [1, 2, 3, 4]
np_archives = np.array(['A', 'B', 'C', 'D'])
for i, f in zip(list_archives, np_archives):
print(i, f)
# Output
# 1 A
# 2 B
# 3 C
# 4 D

最新更新