In Python 2.7, I have two lists of integers:
在Python 2.7中,我有两个整数列表:
x = [1, 3, 2, 0, 2]
y = [1, 2, 2, 3, 1]
I want to create a third list which indicates whether each element in x
and y
is identical, to yield:
我想创建一个第三个列表,指示x和y中的每个元素是否相同,以产生:
z = [1, 0, 1, 0, 0]
How can I do this using list comprehension?
我怎么能用列表理解来做到这一点?
My attempt is:
我的尝试是:
z = [i == j for i,j in ...]
But I don't know how to complete it.
但我不知道如何完成它。
3 个解决方案
#1
22
You are looking for zip
你正在寻找拉链
z = [i == j for i,j in zip(x,y)]
But you better add int
call to get your desired output
但是你最好添加int调用以获得所需的输出
>>> z = [int(i == j) for i,j in zip(x,y)]
>>> z
[1, 0, 1, 0, 0]
else you'll get a list like [True, False, True, False, False]
否则你会得到一个像[真,假,真,假,假]的清单
As ajcr mentions in a comment, it is better to use itertools.izip instead of zip if the lists are very long. This is because it returns an iterator instead of a list. This is mentioned in the documentation
正如ajcr在评论中提到的,如果列表很长,最好使用itertools.izip而不是zip。这是因为它返回迭代器而不是列表。这在文档中提到
Like zip() except that it returns an iterator instead of a list.
像zip()一样,除了它返回迭代器而不是列表。
demo
演示
>>> from itertools import izip
>>> z = [int(i == j) for i,j in izip(x,y)]
>>> z
[1, 0, 1, 0, 0]
#2
3
You can change it a little bit and do:
您可以稍微改变它并执行:
[x[i] == y[i] for i in xrange(len(x))]
If you use Python3 - change xrange
to range
如果您使用Python3 - 将xrange更改为范围
#3
1
While a list comprehension was specified in the question and the answers above are probably better, I thought I'd chime in with a recursive solution:
虽然在问题中指定了列表理解,并且上面的答案可能更好,但我认为我会使用递归解决方案:
def compare_lists(a, b, res=[]):
if len(a) == len(b):
if a == []:
return res
else:
if a[0] == b[0]:
res.append(1)
else:
res.append(0)
return compare_lists(a[1:], b[1:])
else:
return "Lists are of different length."
#1
22
You are looking for zip
你正在寻找拉链
z = [i == j for i,j in zip(x,y)]
But you better add int
call to get your desired output
但是你最好添加int调用以获得所需的输出
>>> z = [int(i == j) for i,j in zip(x,y)]
>>> z
[1, 0, 1, 0, 0]
else you'll get a list like [True, False, True, False, False]
否则你会得到一个像[真,假,真,假,假]的清单
As ajcr mentions in a comment, it is better to use itertools.izip instead of zip if the lists are very long. This is because it returns an iterator instead of a list. This is mentioned in the documentation
正如ajcr在评论中提到的,如果列表很长,最好使用itertools.izip而不是zip。这是因为它返回迭代器而不是列表。这在文档中提到
Like zip() except that it returns an iterator instead of a list.
像zip()一样,除了它返回迭代器而不是列表。
demo
演示
>>> from itertools import izip
>>> z = [int(i == j) for i,j in izip(x,y)]
>>> z
[1, 0, 1, 0, 0]
#2
3
You can change it a little bit and do:
您可以稍微改变它并执行:
[x[i] == y[i] for i in xrange(len(x))]
If you use Python3 - change xrange
to range
如果您使用Python3 - 将xrange更改为范围
#3
1
While a list comprehension was specified in the question and the answers above are probably better, I thought I'd chime in with a recursive solution:
虽然在问题中指定了列表理解,并且上面的答案可能更好,但我认为我会使用递归解决方案:
def compare_lists(a, b, res=[]):
if len(a) == len(b):
if a == []:
return res
else:
if a[0] == b[0]:
res.append(1)
else:
res.append(0)
return compare_lists(a[1:], b[1:])
else:
return "Lists are of different length."