Home > Mobile >  How to build a for loop identing in two different lists at the same time
How to build a for loop identing in two different lists at the same time

Time:01-11

i got a list of archives and a numpy np.array, the files in the list are the same correspondent in the numpy np.array "list"

I would like to know how to make a for loop that iterates in the items of the list like:

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

What I did so far is a for loop that iterates in the list

for i in range(list):
  get_items = the_class_i_need(i, _)

the "_" is where I got confused, because I called the numpy np.array. I know it's not working because I'm not calling the element by itself but the hole 200 itens in there. Somehow I think I was supposed to do a for loop inside a for loop but I learned what I larned all by myself and this is a hard thing to me. So thanks for the help already!!

CodePudding user response:

As mentioned above, you can iterate through your list and np array in parallel using zip() function by doing :

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

For example :

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
  • Related