【发布时间】:2019-09-22 23:04:47
【问题描述】:
我有两个列表
first= (1,2,3,4,5,6)
last=(6,5,4,3,2,1)
我只需要比较相应的值。我使用了下面的代码并获得了 36 个结果,因为第一个元素与最后一个列表的所有六个元素进行了比较。
for x in first:
for y in last:
if x>y:
print("first is greater then L2",y)
elif x==y:
print("equal")
else:
print("first is less then L2",y)
irst= (1,2,3,4,5,6)
last=(6,5,4,3,2,1)
for x in first:
for y in last:
if x>y:
print("first is greater then L2",y)
elif x==y:
print("equal")
else:
print("first is less then L2",y)
输出:
L1 is less then L2 6
L1 is less then L2 5
L1 is less then L2 4
L1 is less then L2 3
L1 is less then L2 2
go dada
L1 is less then L2 6
L1 is less then L2 5
L1 is less then L2 4
L1 is less then L2 3
go dada
L1 is greater then L2 1
L1 is less then L2 6
L1 is less then L2 5
L1 is less then L2 4
go dada
L1 is greater then L2 2
L1 is greater then L2 1
L1 is less then L2 6
L1 is less then L2 5
go dada
L1 is greater then L2 3
L1 is greater then L2 2
L1 is greater then L2 1
L1 is less then L2 6
go dada
L1 is greater then L2 4
L1 is greater then L2 3
L1 is greater then L2 2
L1 is greater then L2 1
go dada
L1 is greater then L2 5
L1 is greater then L2 4
L1 is greater then L2 3
L1 is greater then L2 2
L1 is greater then L2 1
y
我只需要比较相应元素的结果。这意味着应该只有六个输出。
【问题讨论】:
-
(1,2,3,4,5,6)不是一个列表。它是一个元组。 -
这就是 R 击败 python 的原因。
x = c(1, 2, 3) x[x %in% c(2, 3)] = (2, 3)
标签: python python-3.x