Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何比较列表中的相应位置?_Python_Python 2.7 - Fatal编程技术网

Python 如何比较列表中的相应位置?

Python 如何比较列表中的相应位置?,python,python-2.7,Python,Python 2.7,我有N个相同长度的列表。如何比较相应位置的这些列表,并输出它们都匹配的位置数 例如: A=[1,0,1,1] B=[1,1,1,1] C=[1,0,1,0] 比较这三个列表将输出2,因为只有位置1和3匹配 我想把它转换成元组,然后压缩它K=zip(A,B,C),然后添加每个元组,看看它是否匹配列表的数量 这个问题听起来好像我遗漏了一些很琐碎的东西,也许吧 使用set,就像gnibbler的回答一样,适用于包含任何类型的不可变元素的列表——任意整数、浮点数、字符串、元组。OP的问题没有对列表可能

我有N个相同长度的列表。如何比较相应位置的这些列表,并输出它们都匹配的位置数

例如:

A=[1,0,1,1]
B=[1,1,1,1]
C=[1,0,1,0]
比较这三个列表将输出
2
,因为只有位置1和3匹配


我想把它转换成元组,然后压缩它
K=zip(A,B,C)
,然后添加每个元组,看看它是否匹配列表的数量


这个问题听起来好像我遗漏了一些很琐碎的东西,也许吧

使用
set
,就像gnibbler的回答一样,适用于包含任何类型的不可变元素的列表——任意整数、浮点数、字符串、元组。OP的问题没有对列表可能包含的内容施加任何限制,并且假设只有0和1不会简化任何内容。以下内容也适用于任意数量的列表,而不仅仅是3个,这似乎是OP需求的一部分

sum(1 if x == y == z else 0 for x, y, z in zip(A, B, C))
2
A=[1,0,1,1, 'cat', 4.5,     'no']
B=[1,1,1,1, 'cat', False,   'no']
C=[1,0,1,0, 16,    'John',  'no']
D=[1,0,0,0, (4,3), 'Sally', 'no']


def number_of_matching_positions(*lists):
    """
    Given lists, a list of lists whose members are all of the same length,
    return the number of positions where all items in member lists are equal.
    """
    return sum([len(set(t)) <= 1 for t in zip(* lists)])

print(number_of_matching_positions(),
      number_of_matching_positions(A),
      number_of_matching_positions(A, A),
      number_of_matching_positions(A, B),
      number_of_matching_positions(A, B, C),
      number_of_matching_positions(A, B, C, D))

如果您想知道所有列表匹配的位置,那么

list(x for x, (xa, xb, xc) in enumerate(zip(a, b, c))
     if xa == xb == xc)
在我看来,这是最容易阅读的方式

如果你只想数一数列的数目

sum(xa == xb == xc for xa, xb, xc in zip(a, b, c))

由于在Python中,True和False可以在计算中使用,就好像它们是
1
0
一样,我想把它们转换成元组,然后压缩它
K=zip(A,B,C)
,然后添加每个元组,看看它是否与列表的数量匹配。这正是我所想的,但无法用代码表达:)您可能需要对代码进行一些解释,有时理解可能有点难:)@aj8uppal,但这是一个生成器表达式;-)您可以简化为
sum(对于zip(A,B,C)中的x,y,z,x==y==z)
。如果只有三个列表需要比较,那么这是一个很好的编写方法。我喜欢使用
len(set(i))==1来正确地完成我糟糕的工作。@Floris,谢谢。很高兴你没有误解我的批评,一点也没有。你说得对,我的大脑变成了糊状。不幸的是,它给出了正确的答案,使我产生了一种虚假的安全感。确认偏见等等。祝你好运,达到10万-你现在已经很接近了!
list(x for x, (xa, xb, xc) in enumerate(zip(a, b, c))
     if xa == xb == xc)
sum(xa == xb == xc for xa, xb, xc in zip(a, b, c))