简体   繁体   English

如何使用python连接/合并两个生成器输出

[英]How to join/merge two generators output using python

I have two generators g1 and g2 我有两台发电机g1和g2

for line in g1:
    print line[0]

[a, a, a] [a,a,a]
[b, b, b] [b,b,b]
[c, c, c] [c,c,c]

for line1 in g2:
    print line1[0]

[1, 1, 1] [1,1,1]
[2, 2, 2] [2,2,2]
[3, 3, 3] [3,3,3]

for line in itertools.chain(g1, g2):
    print line[0]

[a, a, a] [a,a,a]
[b, b, b] [b,b,b]
[c, c, c] [c,c,c]
[1, 1, 1] [1,1,1]
[2, 2, 2] [2,2,2]
[3, 3, 3] [3,3,3]


How 怎么样

do I get the output like: 我得到的输出如下:

[a, a, a],[1, 1, 1] [a,a,a],[1,1,1]
[b, b, b],[2, 2, 2] [b,b,b],[2,2,2]
[c, c, c],[3, 3, 3] [c,c,c],[3,3,3]

or 要么

[a, a, a, 1, 1, 1] [a,a,a,1,1,1]
[b, b, b, 2, 2, 2] [b,b,b,2,2,2]
[c, c, c, 3, 3, 3] [c,c,c,3,3,3]


Thank You for Your help. 谢谢您的帮助。

first case: use 第一种情况:使用

for x, y in zip(g1, g2):
    print(x[0], y[0])

second case: use 第二种情况:使用

for x, y in zip(g1, g2):
    print(x[0] + y[0])

You can of course use itertools.izip for the generator version. 您当然可以使用itertools.izip作为生成器版本。 You get the generator automatically if you use zip in Python 3 and greater. 如果在Python 3及更高版本中使用zip ,则会自动获得生成器。

You can use itertools.izip for example 例如,您可以使用itertools.izip

g1=([s]*3 for s in string.ascii_lowercase)
g2=([s]*3 for s in string.ascii_uppercase)
g=itertools.izip(g1,g2)

This will ensure the resultant is also a generator. 这将确保结果也是发电机。

If you prefer to use the second here is how you can do it 如果你喜欢在这里使用第二个是你如何做到这一点

g1=([s]*3 for s in string.ascii_lowercase)
g2=([s]*3 for s in string.ascii_uppercase)
g=(x+y for x,y in itertools.izip(g1,g2))

You can get pairs of things (your first request) using zip(g1, g2) . 你可以使用zip(g1, g2)获得成对的东西(你的第一个请求zip(g1, g2) You can join them (your second request) by doing [a + b for a, b in zip(g1, g2)] . 你可以通过[a + b for a, b in zip(g1, g2)]加入它们(你的第二个请求)。

Almost equivalently, you can use map . 几乎等效,你可以使用map Use map(None, g1, g2) to produce a list of pairs, and map(lambda x, y: x + y, g1, g2) to join the pairs together. 使用map(None, g1, g2)生成对列表,并map(lambda x, y: x + y, g1, g2)以将这些对连接在一起。

In your examples, your generators are producing a list or tuple each time, of which you're only interested in the first element. 在您的示例中,您的生成器每次都生成一个列表或元组,其中您只对第一个元素感兴趣。 I'd just generate the thing you need, or preprocess them before zipping or mapping them. 我只是生成你需要的东西,或者在压缩或映射之前预处理它们。 For example: 例如:

g1 = (g[0] for g in g1)
g2 = (g[0] for g in g2)

Alternatively, you can apply [0] in the map. 或者,您可以在地图中应用[0]。 Here's the two cases: 这是两种情况:

map(lambda x, y: (x[0], y[0]), g1, g2)
map(lambda x, y: x[0] + y[0], g1, g2)

Let's say you have g1 and g2 : 假设你有g1g2

g1 = [
    [['a', 'a', 'a'], ['e', 'e'], ['f', 'g']],
    [['b', 'b', 'b'], ['e', 'e'], ['f', 'g']],
    [['c', 'c', 'c'], ['e', 'e'], ['f', 'g']],
]

g2 = [
    [[1, 1, 1], ['t', 'q'], ['h', 't']],
    [[2, 2, 2], ['r', 'a'], ['l', 'o']],
    [[3, 3, 3], ['x', 'w'], ['z', 'p']],
]

To get that : 为此:

[a, a, a],[1, 1, 1]
[b, b, b],[2, 2, 2]
[c, c, c],[3, 3, 3]

You can do that : 你可以这样做:

result1 = map(lambda a, b: (a[0], b[0]) , g1, g2)
# Which is like this :
[(['a', 'a', 'a'], [1, 1, 1]),
 (['b', 'b', 'b'], [2, 2, 2]),  
 (['c', 'c', 'c'], [3, 3, 3])]

And for the second : 而对于第二个:

[a, a, a, 1, 1, 1]
[b, b, b, 2, 2, 2]
[c, c, c, 3, 3, 3]

result2 = map(lambda a, b: a[0]+b[0] , g1, g2)
# Which is like that :
[['a', 'a', 'a', 1, 1, 1],
 ['b', 'b', 'b', 2, 2, 2],
 ['c', 'c', 'c', 3, 3, 3]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM