I have two generators g1 and g2
for line in g1:
print line[0]
[a, a, a]
[b, b, b]
[c, c, c]
for line1 in g2:
print line1[0]
[1, 1, 1]
[2, 2, 2]
[3, 3, 3]
for line in itertools.chain(g1, g2):
print line[0]
[a, a, a]
[b, b, b]
[c, c, c]
[1, 1, 1]
[2, 2, 2]
[3, 3, 3]
How
do I get the output like:
[a, a, a],[1, 1, 1]
[b, b, b],[2, 2, 2]
[c, c, c],[3, 3, 3]
or
[a, a, a, 1, 1, 1]
[b, b, b, 2, 2, 2]
[c, c, c, 3, 3, 3]
Thank You for Your help.
In python, we can use the + operator to merge the contents of two lists into a new list. For example, We can use + operator to merge two lists i.e. It returned a new concatenated lists, which contains the contents of both list_1 and list_2.
Create Generators in Python It is fairly simple to create a generator in Python. It is as easy as defining a normal function, but with a yield statement instead of a return statement. If a function contains at least one yield statement (it may contain other yield or return statements), it becomes a generator function.
Generator functions in Python are used to create iterators and return an iterator object. In the generator function, we use a yield statement instead of a return statement. We can use multiple yield statements in the generator function.
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. You get the generator automatically if you use zip
in Python 3 and greater.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With