R_ga
R_ga

Reputation: 3

Comparing two arrays with For loop with python

Hello new here in stackoverflow, also I am new with the programation in Python and still learning.

I want to know why I am getting a Syntax error in the second for loop, I am trying to compare two arrays of the same lenght, when ax > bx A recive 1 point, when ax < bx B recive 1 point and where ax == bx nobody get points.

def solve(a0, a1, a2, b0, b1, b2):
  A = 0
  B = 0
  a = [a0 , a1 ,a2]
  b = [b0, b1, b2]
  for x in a and for y in b:
    if x > y:
        pointA + 1
    if x==y:
        pass
    else:
        pointB + 1
  result = [pointA, pointB]
  return result


a0, a1, a2 = raw_input().strip().split(' ')
a0, a1, a2 = [int(a0), int(a1), int(a2)]
b0, b1, b2 = raw_input().strip().split(' ')
b0, b1, b2 = [int(b0), int(b1), int(b2)]
result = solve(a0, a1, a2, b0, b1, b2)
print " ".join(map(str, result))

then with some Investigation I tried:

from itertools import product
import sys


def solve(a0, a1, a2, b0, b1, b2):
  A = 0
  B = 0
  a = [a0 , a1 ,a2]
  b = [b0, b1, b2]
  A = sum(1 if x>y else 0 for x, y in product(a, b))
  B = sum(1 if x<y else 0 for x, y in product(a, b))
  result = [A, B]
  return result

a0, a1, a2 = raw_input().strip().split(' ')
a0, a1, a2 = [int(a0), int(a1), int(a2)]
b0, b1, b2 = raw_input().strip().split(' ')
b0, b1, b2 = [int(b0), int(b1), int(b2)]
result = solve(a0, a1, a2, b0, b1, b2)
print " ".join(map(str, result))

but when the Input is:

1 1 1
0 0 0

I got:

9 0

Can someone explain what I am doing wrong and why? Thank you in advance.

Regards, R

Upvotes: 0

Views: 4611

Answers (1)

gonczor
gonczor

Reputation: 4146

and expects boolean values not expressions, so this: for x in a and for y in b: will never work.

You could use zip():

1 a
2 b
3 c
>>> a = [1, 2, 3]
>>> b = ['a', 'b', 'c']
>>> for x, y in zip(a, b):
...   print('{} {}'.format(x, y))
... 
1 a
2 b
3 c

And please correct your indentations.

Upvotes: 1

Related Questions