程序应该返回一个2值x,y的value[],给出列表中值的比较。
例如,如果a> b,则分判给x,反之亦然。如果a1 == b1,则不应被授予积分。在最后一个值被迭代之后,最后的分数应该是1,1。
def return_score_comparison():
a = [2, 4, 6]
b = [3, 4, 5]
x = 0
y = 0
score_ = [x, y]
noPoint = 0
for m in range(len(a)):
for n in range(len(b)):
if a[m] > b[n]:
x = x + 1
elif a[m] < b[n]:
y = y + 1
else:
a[m] == b[n]
print(noPoint)
return score_发布于 2021-10-29 06:09:20
在多个列表的成对(并行)迭代中使用zip。嵌套循环为您提供了跨乘积(将a的每个元素与来自b的每个元素结合在一起,而不仅仅是在其特定索引处的元素):
def score(a, b):
score_a = score_b = 0
for x, y in zip(a, b):
score_a += (x > y)
score_b += (y > x)
return [score_a, score_b]
a = [2, 4, 6]
b = [3, 4, 5]
score(a, b)
# [1, 1]https://stackoverflow.com/questions/69764317
复制相似问题