python - 在python列表中查找相同索引的不匹配项

标签 python list

我有以下列表

l1 = ['a','b','c','a']
l2 = ['a','d','c','c']

我想找出 l2 中与 l1 中同一索引处的元素不匹配的元素。 例如:上面列表的输出将是 ['d','c']

因为 l2 应该在第二个位置有 'b'。

我可以通过遍历列表并找到不匹配的地方来做到这一点。

l3 = []
for i in range(len(l1)):
    if l1[i] != l2[i]: l3.append(l2[i])
print l3

有没有更好的方法来做到这一点。 谢谢。

最佳答案

missing = [b for a,b in itertools.izip_longest(l1,l2,fillvalue=object()) if a != b]

有点pythonic ...但基本上是一样的

关于python - 在python列表中查找相同索引的不匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26069659/

相关文章:

python - 使用 PySpark 删除 spark 数据框中嵌套结构中的列(文本中的详细信息)

python - fatal error C1083 : Cannot open include file: 'openssl/opensslv.h'

python - 如何在python中创建高效的同现?

list - Haskell 得到一个过滤的整数列表

python - 根据其中一个值从元组列表中删除重复项

python - Tensorflow model.fit() 对于所有浮点输入返回 NaN

python - 如何使从 map 边缘生成的对象沿着对角线向中间移动?

python - 如何使用 cx_oracle django 包连接到 oracle 遗留数据库?

将唯一列表中的 NA 替换为 R 中第二个列表中相同位置的 NA

Python:遍历列表