3

I have two string arrays each with three columns.I want to compare first two columns of both 2-d arrays(having 3 cols and 4000 rows). if they match then i need those matching values.But my code is not working.Here is a sample.

array1=["1stcolumn...", "2ndColumn...", "3rdColumn..."]
array2=[1stcolumn 2ndColumn 3rdColumn]
if (array1[0]==array2[0] and array1[1]==array2[1]):
       array3.append('matches: {!r}'.format(array1))
print(array3)
Nisa
  • 227
  • 3
  • 10

3 Answers3

1

EDIT

if array1[:2] == array2[:2]: compares all items from the index 0 to 2(2 is not included), and comes up with the same result as if array1[0] == array2[0] and array1[1] == array2[1]:. Also, it is simpler.(Thanks to Wyatt for comment)

If your arrays are 2-dimensional:

def compare_columns(array1, array2):
    if len(array1) != len(array2):
        return False # If row numbers are not same, return false

    for row_number in range(len(array1)):
        if array1[row_number][:2] != array2[row_number][:2]:
            return False # If the content is not equal, return false

    return True # All of the content is equal, the return true

# For example, these are 2-dimensional arrays
array1 = [["1.1", "1.2", "Lord of the Day of Judgment!"],
          ["2.1", "2.2", "Lord of the Day of Judgment!"]]
array2 = [["1.1", "1.2", "مَالِكِ يَوْمِ الدِّينِ"],
          ["2.1", "2.2", "مَالِكِ يَوْمِ الدِّينِ"]]

array3 = []       
if compare_columns(array1, array2):
       array3.append('matches: {!r}'.format(array1))
print(array3)

Output:

["matches: [['1.1', '1.2', 'Lord of the Day of Judgment!'], ['2.1', '2.2', 'Lord of the Day of Judgment!']]"]

BEFORE EDIT:

If your array is one dimensionel, you don't need to say column, it is just item. Then your job is easy like you have done above. Just, you have a few syntax errors. Use this code:

array1 = ["1stcolumn", "2ndColumn", "1-3rdColumn"]
array2 = ["1stcolumn", "2ndColumn", "2-3rdColumn"]
array3 = []
if array1[0] == array2[0] and array1[1] == array2[1]:
       array3.append('matches: {!r}'.format(array1))
print(array3)

Output:

["matches: ['1stcolumn', '2ndColumn', '1-3rdColumn']"]

So, if you have any other problem, let us know.

Alperen
  • 3,772
  • 3
  • 27
  • 49
  • The comparison can also be done with `array1[:2] == array2[:2]`. –  Oct 21 '17 at 20:49
  • My array is 2 dimensional.Both arrays have 3 columns and 4000 rows . So, how can we match with 2d array? – Nisa Oct 21 '17 at 20:52
  • Yes, actually, this is a better way. But, I didn't want to change the original code much. – Alperen Oct 21 '17 at 20:52
  • @Nisa I edited my answer for 2-dimesional arrays. If my answer doesn't work and your problem continue, share a part of your arrays, then let us know more details. – Alperen Oct 21 '17 at 21:30
  • Thank you..But your code is checking whether number of rows are equal or not..i need to compare contents of first 2 columns of both arrays and retrieve the matching indexes and i also need to loop through all the rows of array to compare.. Here are my sample arrays: array1=[''1","4","Lord of the Day of Judgment!''] array2=["1","4","مَالِكِ يَوْمِ الدِّينِ"] – Nisa Oct 22 '17 at 10:07
  • Yes my code is checking whether number of rows are equal or not, because if it is not equal, you can't compare columns. After checking the equality of row numbers, the function compares content of columns in for loop. For loop is inside the function. If you you try the function with different inputs, you will see it works. – Alperen Oct 22 '17 at 13:44
1

There are a number of errors in this code snippet which will prevent it from even running without errors.

  • Python lists are declared with commas between elements. For example, a declaration of a list of strings could be:

    array1 = ["this", "is", "a", "list"]

    Examples of using lists in Python (3) can be found here.

  • The Python logical 'and' operator is not &. It is and. See this question.

  • In Python, as in most languages, variables must be declared before they can be referenced. In your code, array3 is never declared. You can always declare an empty list like this:

    array3 = []

L.Grozinger
  • 2,280
  • 1
  • 11
  • 22
0

As always we need a sample data set

In [1]: from random import randint

In [2]: a = [[randint(0, 1) for _ in range(3)] for __ in range(10)]

In [3]: b = [[randint(0, 1) for _ in range(3)] for __ in range(10)]

Have a look at it

In [4]: for aa, bb in zip(a, b): print(aa, bb)
[1, 1, 0] [0, 1, 0]
[0, 0, 0] [1, 0, 0]
[1, 1, 0] [1, 1, 0]
[1, 1, 0] [0, 1, 0]
[0, 0, 0] [1, 0, 0]
[0, 0, 0] [1, 0, 1]
[1, 1, 1] [1, 1, 1]
[0, 1, 0] [1, 0, 0]
[1, 0, 1] [1, 0, 1]
[1, 1, 1] [1, 0, 0]

It seems that there are a few candidates... let's see if we can sort out the sub-lists of the first list where the first 2 elements are equal to the corresponding elements of the corresponding sub-list in the second list.

A possible solution involves a list comprehension, using zip to pair corresponding sub-lists and filtering according to our criterium:

In [5]: c = [aa for (aa, bb) in zip(a, b) if aa[:2]==bb[:2]]

where the comparison is done on two slices of the sub-lists, avoiding the use of the logical operator and.

Comparing c with the dump of a and b (see input cell #4)

In [6]: c
Out[6]: [[1, 1, 0], [1, 1, 1], [1, 0, 1]]

In [7]: 

it seems to me that the list comprehension procedure here proposed is correct.

gboffi
  • 22,939
  • 8
  • 54
  • 85