迭代两个序列 [英] Iteration over two sequences
问题描述
我刚刚开始学习Python,主要是通过在Dive Into Python和玩游戏中的示例
。
很多时候,我发现需要在
的同时迭代两个序列,我有点难以找到一种方法在
" pythonic"时尚。一个例子是点积。这样直接的&b
类似的方式是:
def dotproduct(a,b):
psum = 0
我在范围内(len(a)):
psum + = a [i] * b [i]
返回psum
但是,范围(len(a))的术语非常不同于pythonic :)
内置函数map()给了我一种转置的方式列表
和b列表,现在我可以用列表理解来处理它:
def dotproduct(a,b):
返回总和([x * y表示x,y在地图中(无,a,b)])
我关心的是效率:在我看来我有两个循环
那里:第一个隐含地图(...)然后是for循环 -
似乎是浪费,因为我觉得我应该能够通过参数映射来进行
乘法运算。到目前为止,我通过定义一个单独的函数提出了一个
替代方案:
def dotproduct(a,b):
def prod(x,y):返回x * y
返还金额(地图(prod,a,b))
我想我也可以在这里使用lambda - 但是有一个不同的,
高效且明显的解决方案我可以忽略吗?
谢谢,
Henrik
-
在一个伟大而光荣的日子里,这片土地上的普通人将会在他们的心中达到
最后的愿望和白宫将由
a彻头彻尾的白痴装饰。
-HL Mencken(1880-1956)美国作家
I am just starting to learn Python, mostly by going through the examples
in Dive Into Python and by playing around.
Quite frequently, I find the need to iterate over two sequences at the
same time, and I have a bit of a hard time finding a way to do this in a
"pythonic" fashion. One example is a dot product. The straight-ahead
C-like way of doing it would be:
def dotproduct(a, b):
psum = 0
for i in range(len(a)):
psum += a[i]*b[i]
return psum
However, the range(len(a)) term is awfully un-pythonic :)
The built-in function map() gives me a way of "transposing" the a list
and the b list, and now I can handle it with a list comprehension:
def dotproduct(a, b):
return sum([x*y for x, y in map(None, a, b)])
My concern is one of efficiency: it seems to me that I have two loops
there: first one implied with map(...) and then the for loop -- which
seems like a waste since I feel I should be able to do the
multiplication via an argument to map. So far I have come up with an
alternative via defining a separate function:
def dotproduct(a, b):
def prod(x,y): return x*y
return sum(map(prod, a, b))
I suppose I could also use a lambda here -- but is there a different,
efficient, and obvious solution that I''m overlooking?
Thanks,
Henrik
--
"On some great and glorious day the plain folks of the land will reach
in their heart''s desire at last and the White House will be adorned by
a downright moron."
-H.L. Mencken (1880-1956) American Writer
推荐答案
zip或izip是你的朋友:
import itertools
a = [1,2,3]
b = [''a'',''b'',''c'']
for a,b in itertools.izip(a,b):
打印a,b
-
问候,
Diez B. Roggisch
zip or izip is your friend:
import itertools
a = [1,2,3]
b = [''a'', ''b'', ''c'']
for a,b in itertools.izip(a, b):
print a, b
--
Regards,
Diez B. Roggisch
" Henrik Holm" <是ne ******* @ henrikholm.com>在消息中写道
news:1gq9qs9.3snutr1s4mcn2N%ne ******* @ henrikholm.c om ...
"Henrik Holm" <ne*******@henrikholm.com> wrote in message
news:1gq9qs9.3snutr1s4mcn2N%ne*******@henrikholm.c om...
我刚刚开始学习Python,主要是通过在潜水到Python中的例子和玩游戏。
很多时候,我发现需要在
同时迭代两个序列,我有在pythonic中找到一种方法可能会有点困难。时尚。一个例子是点积。这样直接的C-like方式是:
def dotproduct(a,b):
psum = 0
我在范围内( len(a)):
psum + = a [i] * b [i]
返回psum
然而,范围(len(a))的术语非常糟糕-pythonic :)
内置函数map()为我提供了一种transposing方式。列表
和b列表,现在我可以用列表理解来处理它:
def dotproduct(a,b):
返回总和([x * y for x,y in map(None,a,b)])
我关心的是效率问题:在我看来,我有两个循环
那里:第一个隐含地图(...)然后for循环 - 这似乎是一种浪费,因为我觉得我应该能够通过参数映射进行乘法运算。到目前为止,我通过定义一个单独的函数提出了一个
替代方案:
def dotproduct(a,b):
def prod(x,y):return x * y
返回总和(地图(prod,a,b))
我想我也可以在这里使用lambda - 但是有一个不同的,有效的,明显的我正在忽视的解决方案?
谢谢,
Henrik
-
在一些伟大而光荣的日子里,土地将最终以他们心中的愿望达到,白宫将由一个彻头彻尾的白痴装饰。
-HL Mencken(1880-1956)美国作家
I am just starting to learn Python, mostly by going through the examples
in Dive Into Python and by playing around.
Quite frequently, I find the need to iterate over two sequences at the
same time, and I have a bit of a hard time finding a way to do this in a
"pythonic" fashion. One example is a dot product. The straight-ahead
C-like way of doing it would be:
def dotproduct(a, b):
psum = 0
for i in range(len(a)):
psum += a[i]*b[i]
return psum
However, the range(len(a)) term is awfully un-pythonic :)
The built-in function map() gives me a way of "transposing" the a list
and the b list, and now I can handle it with a list comprehension:
def dotproduct(a, b):
return sum([x*y for x, y in map(None, a, b)])
My concern is one of efficiency: it seems to me that I have two loops
there: first one implied with map(...) and then the for loop -- which
seems like a waste since I feel I should be able to do the
multiplication via an argument to map. So far I have come up with an
alternative via defining a separate function:
def dotproduct(a, b):
def prod(x,y): return x*y
return sum(map(prod, a, b))
I suppose I could also use a lambda here -- but is there a different,
efficient, and obvious solution that I''m overlooking?
Thanks,
Henrik
--
"On some great and glorious day the plain folks of the land will reach
in their heart''s desire at last and the White House will be adorned by
a downright moron."
-H.L. Mencken (1880-1956) American Writer
zip可能吗?
def dotproduct(a,b):
返回总和([x * y表示x,y表示拉链(a,b)])
- Paul
zip maybe?
def dotproduct(a,b):
return sum([x*y for x,y in zip(a,b)])
-- Paul
" Henrik Holm" <是ne ******* @ henrikholm.com>在消息中写道
新闻:1gq9qs9.3snutr1s4mcn2N%ne ******* @ henrikholm.c om ...
"Henrik Holm" <ne*******@henrikholm.com> wrote in message
news:1gq9qs9.3snutr1s4mcn2N%ne*******@henrikholm.c om...
我想我也可以在这里使用lambda - 但是有一个不同的,有效的,明显的解决方案我可以忽略吗?
I suppose I could also use a lambda here -- but is there a different,
efficient, and obvious solution that I''m overlooking?
检查中的itertools配方图书馆文件。
Check the itertools recipes in the library documentation.
这篇关于迭代两个序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!