user3149650
user3149650

Reputation: 203

loop through two multidimensional arrays

I have two multiD lists

 list one [["hello","how", "are", "you"]] and
 list two [["ss", "gg", "ff"]]

I want to compare EACH of the rows in list one with all of the rows values in list two.

Example

If list one has 2 rows with arrays list[2][values]
and list two has 3 rows with arrays list[3][values]
then

list one [0][all values] compare with
     list two[0][values],
     list two[1][values],
     list two[2][values], and
     list two[3][values].
Then take row two of list one and compare it with
     all rows in list two again
     and so on.

how could that be done?

Upvotes: 1

Views: 441

Answers (3)

afkfurion
afkfurion

Reputation: 2865

Basically, it's nested for loop. You can use list comprehension to do that.

matches = [(A.index(a),B.index(b)) for a in A for b in B if len(set(a).intersection(set(b)))]

Upvotes: 3

Nat
Nat

Reputation: 3077

So given

A = [[1,2,3],[4,5,6],[7,8,9]]
B = [[11,12,13],[14,15,16],[17,18,19]]

Is this what you want?

for a_row in A:
  for b_row_i in xrange(len(B)):
    print 'compare', a_row, B[b_row_i]

Upvotes: 0

fuesika
fuesika

Reputation: 3330

Since your question is pseudo-code, I'll respond the same way. ;-)

for all rows in list1:
    for all rows in list2:
        %comparison of rows..
        matches = set(current row of list1) & set(current row of list2)

Basically, this SO question/answer should cover your problem. Possibly, there is a more efficient way to the solution. I'm a beginner with python myself.

Upvotes: 0

Related Questions