0

Say if I had multiple arrays in python. For example,

r1 = [a1[0], a2[0], a3[0], a4[0], a5[0], a6[0], a7[0], a8[0], a9[0]]
r2 = [a1[1], a2[1], a3[1], a4[1], a5[1], a6[1], a7[1], a8[1], a9[1]]
r3 = [a1[2], a2[2], a3[2], a4[2], a5[2], a6[2], a7[2], a8[2], a9[2]]
r4 = [a1[3], a2[3], a3[3], a4[3], a5[3], a6[3], a7[3], a8[3], a9[3]]
r5 = [a1[4], a2[4], a3[4], a4[4], a5[4], a6[4], a7[4], a8[4], a9[4]]
r6 = [a1[5], a2[5], a3[5], a4[5], a5[5], a6[5], a7[5], a8[5], a9[5]]
r7 = [a1[6], a2[6], a3[6], a4[6], a5[6], a6[6], a7[6], a8[6], a9[6]]
r8 = [a1[7], a2[7], a3[7], a4[7], a5[7], a6[7], a7[7], a8[7], a9[7]]
r9 = [a1[8], a2[8], a3[8], a4[8], a5[8], a6[8], a7[8], a8[8], a9[8]]

This makes a grid of numbers, but is there a way to scan through all of these lists? So far I sort of came up with this pseudocode:

"for x in multiple lists" 
     if any element at any of the r lists of the list ==0
     Do something
4
  • 1
    That's not valid python. Commented Jan 27, 2016 at 21:00
  • Look at zip. It will make your life enormously easier. Commented Jan 27, 2016 at 21:01
  • chain from itertools may also be handy Commented Jan 27, 2016 at 21:39
  • Are you using that kind of data structure because of an assignment requirement, or are you open to using something else to make it easier? If so you should check out numpy Commented Jan 27, 2016 at 21:42

3 Answers 3

2

You an use a nested generator expression within built in function any() for that aim:

if any(item == 0 for sub_list in r_lists for item in sub_lists):
      # do stuff
Sign up to request clarification or add additional context in comments.

Comments

0

You can iterate:

list_of_lists = [r1, r2, r3, r4, r5, r6, r7, r8, r9]
for row in list_of_lists:
    if any(element == 0 for element in row):
        do_something()

Comments

0

This looks like a two-dimensional array really. Not sure, what you are trying to do, maybe this will help: https://docs.python.org/2/library/array.html ??

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.