0
你好在這裏新的stackoverflow,我也是新的與Python的編程和仍在學習。比較兩個數組與For循環與python
我想知道爲什麼我在第二個循環中出現語法錯誤,我試圖比較兩個相同長度的數組,當ax> bx A recive 1 point,when ax < bx B recive 1 point和ax == bx在哪裏沒有人得分。
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))
然後用一些調查我想:
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))
但是當輸入爲:
1 1 1
0 0 0
我:
9 0
有人能解釋我是什麼做錯了,爲什麼?先謝謝你。
問候, [R
我在帖子中進行了更正,我用zip,現在我的代碼工作真的很好! 謝謝:) –
很高興我能幫到你。 – gonczor