python 比较 2 个不同长度列表中的项目 - 顺序很重要
python compare items in 2 list of different length - order is important
list_1 = ['a', 'a', 'a', 'b']
list_2 = ['a', 'b', 'b', 'b', 'c']
所以在上面的列表中,只有索引 0 中的项目是相同的,而两个列表中的索引 1 到 4 是不同的。另外,list_2
有一个额外的项目 'c'
。
我想计算两个列表中索引不同的次数,在这种情况下我应该得到 3。
我试过这样做:
x = 0
for i in max(len(list_1),len(list_2)):
if list_1[i]==list_2[i]:
continue
else:
x+=1
我收到一个错误。
使用zip()
function对列表进行配对,计算所有差异,然后添加长度差异。
zip()
只会迭代可以配对的项目,但迭代其余部分没有什么意义;你知道这些都算作不同:
differences = sum(a != b for a, b in zip(list_1, list_2))
differences += abs(len(list_1) - len(list_2))
sum()
总结了 True
和 False
值;这是可行的,因为 Python 的 boolean
类型是 int
的子类,并且 False
等于 0
,True
等于 1
。因此,对于每对不同的元素,!=
测试产生的 True
值加起来为 1
s。
演示:
>>> list_1 = ['a', 'a', 'a', 'b']
>>> list_2 = ['a', 'b', 'b', 'b', 'c']
>>> sum(a != b for a, b in zip(list_1, list_2))
2
>>> abs(len(list_1) - len(list_2))
1
>>> difference = sum(a != b for a, b in zip(list_1, list_2))
>>> difference += abs(len(list_1) - len(list_2))
>>> difference
3
正如 ZdaR 评论的那样,您应该得到 3 作为结果,如果列表中没有 None
,zip_longest
可以提供帮助。
from itertools import zip_longest
list_1=['a', 'a', 'a', 'b']
list_2=['a', 'b', 'b', 'b', 'c']
x = sum(a != b for a,b in zip_longest(list_1,list_2))
我可以使用 for 循环尝试这种方式吗:
>>> count = 0
>>> ls1 = ['a', 'a', 'a', 'b']
>>> ls2 = ['a', 'b', 'b', 'b', 'c']
>>> for i in range(0, max(len(ls1),len(ls2)), 1):
... if ls1[i:i+1] != ls2[i:i+1]:
... count += 1
...
>>> print count
3
>>>
你可以试试这个:
list1 = [1,2,3,5,7,8,23,24,25,32]
list2 = [5,3,4,21,201,51,4,5,9,12,32,23]
list3 = []
for i in range(len(list2)):
if list2[i] not in list1:
pass
else :
list3.append(list2[i])
print list3
print len(list3)
或者试试这个(不改变列表):
dif = 0
for i in range(len(min(list_1, list_2))):
if list_1[i]!=list_2[i]:
dif+=1
#print(list_1[i], " != ", list_2[i], " --> Dif = ", dif)
dif+=(len(max(list_1, list_2)) - len(min(list_1, list_2)))
print("Difference = ", dif)
(输出:Difference = 3
)
好不了多少,但还有另一个选择
if len(a) < len(b):
b = b[0:len(a)]
else:
a = a[0:len(b)]
correct = sum(a == b)
list_1 = ['a', 'a', 'a', 'b']
list_2 = ['a', 'b', 'b', 'b', 'c']
所以在上面的列表中,只有索引 0 中的项目是相同的,而两个列表中的索引 1 到 4 是不同的。另外,list_2
有一个额外的项目 'c'
。
我想计算两个列表中索引不同的次数,在这种情况下我应该得到 3。
我试过这样做:
x = 0
for i in max(len(list_1),len(list_2)):
if list_1[i]==list_2[i]:
continue
else:
x+=1
我收到一个错误。
使用zip()
function对列表进行配对,计算所有差异,然后添加长度差异。
zip()
只会迭代可以配对的项目,但迭代其余部分没有什么意义;你知道这些都算作不同:
differences = sum(a != b for a, b in zip(list_1, list_2))
differences += abs(len(list_1) - len(list_2))
sum()
总结了 True
和 False
值;这是可行的,因为 Python 的 boolean
类型是 int
的子类,并且 False
等于 0
,True
等于 1
。因此,对于每对不同的元素,!=
测试产生的 True
值加起来为 1
s。
演示:
>>> list_1 = ['a', 'a', 'a', 'b']
>>> list_2 = ['a', 'b', 'b', 'b', 'c']
>>> sum(a != b for a, b in zip(list_1, list_2))
2
>>> abs(len(list_1) - len(list_2))
1
>>> difference = sum(a != b for a, b in zip(list_1, list_2))
>>> difference += abs(len(list_1) - len(list_2))
>>> difference
3
正如 ZdaR 评论的那样,您应该得到 3 作为结果,如果列表中没有 None
,zip_longest
可以提供帮助。
from itertools import zip_longest
list_1=['a', 'a', 'a', 'b']
list_2=['a', 'b', 'b', 'b', 'c']
x = sum(a != b for a,b in zip_longest(list_1,list_2))
我可以使用 for 循环尝试这种方式吗:
>>> count = 0
>>> ls1 = ['a', 'a', 'a', 'b']
>>> ls2 = ['a', 'b', 'b', 'b', 'c']
>>> for i in range(0, max(len(ls1),len(ls2)), 1):
... if ls1[i:i+1] != ls2[i:i+1]:
... count += 1
...
>>> print count
3
>>>
你可以试试这个:
list1 = [1,2,3,5,7,8,23,24,25,32]
list2 = [5,3,4,21,201,51,4,5,9,12,32,23]
list3 = []
for i in range(len(list2)):
if list2[i] not in list1:
pass
else :
list3.append(list2[i])
print list3
print len(list3)
或者试试这个(不改变列表):
dif = 0
for i in range(len(min(list_1, list_2))):
if list_1[i]!=list_2[i]:
dif+=1
#print(list_1[i], " != ", list_2[i], " --> Dif = ", dif)
dif+=(len(max(list_1, list_2)) - len(min(list_1, list_2)))
print("Difference = ", dif)
(输出:Difference = 3
)
好不了多少,但还有另一个选择
if len(a) < len(b):
b = b[0:len(a)]
else:
a = a[0:len(b)]
correct = sum(a == b)