[英]Python: Compare elements in two list by pair
I am so sorry for asking so silly question. 我很抱歉提出如此愚蠢的问题。 So I have got two lists(columns) for instance: 因此,我有两个列表(列),例如:
In:
a= [0.0,1.0,2.0,3.0,4.0]
b= [1.0,2.0,3.0,5.0,6.0]
zp = list(zip(a,b)) #And I zipped it for better view.
for i in zp:
print (i)
Out:
(0.0, 1.0)
(1.0, 2.0)
(2.0, 3.0)
(3.0, 5.0)
(4.0, 6.0)
I would like compare each i[1] with each i[0] in the next pair(tuple) For example: 我想比较下一个对(元组)中的每个i [1]与每个i [0]例如:
1st pair i[1] = 1.0 compare with i[0] in 2nd
2nd pair i[0] = 1.0 compare with i[0] in 3rd
etc
I would like find difference in pair. 我想找一对。
If i[1] != i[0]
print this value
Answer is 5.0 & 4.0
Thank you for your attention 感谢您的关注
The initial zip for better view is not completely necessary. 最初的zip以获得更好的视图并不是完全必要的。
You could simply zip
a slice of a
starting from index 1 with b
, and then compare the items using a for
loop: 你可以简单地zip
切片a
从索引1开始b
,然后比较使用的物品for
循环:
a = [0.0,1.0,2.0,3.0,4.0]
b = [1.0,2.0,3.0,5.0,6.0]
for i, j in zip(a[1:], b):
if i != j:
print(j, i)
# 5.0, 4.0
index variant without zip and enumerate 不带zip和枚举的索引变体
a = [0.0,1.0,2.0,3.0,4.0]
b = [1.0,2.0,3.0,5.0,6.0]
for i in a:
if a.index(i) and i != b[a.index(i)-1]:
print "{0} > {1}".format(i, b[a.index(i)-1])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.