Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting <generator object <genexpr>

I have 2 lists:

first_lst = [('-2.50', 0.49, 0.52), ('-2.00', 0.52, 0.50)]
second_lst = [('-2.50', '1.91', '2.03'), ('-2.00', '1.83', '2.08')]

I want to do the following math to it:

Multiply 0.49 by 1.91 (the corresponding values from first_lst and second_lst), and multiply 0.52 by 2.03 (corresponding values also). I want to do that under condition that values at position 0 in each corresponding tuple is idential so -2.50 == -2.50 etc. Obviously, we do the same math for remaning tuples as well.

My code:

[((fir[0], float(fir[1])*float(sec[1]), float(fir[2])*float(sec[2])) for fir in first_lst) for sec in second_lst if fir[0] == sec[0]]

Generates however some object:

[<generator object <genexpr> at 0x0223E2B0>]

Can you help me fix the code?

like image 981
nutship Avatar asked May 04 '13 10:05

nutship


People also ask

What is a generator expression?

This version opens a file, loops through each line, and yields each row, instead of returning it. You can also define a generator expression (also called a generator comprehension ), which has a very similar syntax to list comprehensions. In this way, you can use the generator without calling a function:

How do you know if a list is a generator?

Take a look at what happens when you inspect each of these objects: The first object used brackets to build a list, while the second created a generator expression by using parentheses. The output confirms that you’ve created a generator object and that it is distinct from a list.

Why is my generator expression not working?

There are 2 issues to look at. and <generator object <genexpr> message is mentioned. 1) Let's fix the the code with minimum amount of changes by creating list comprehension: 2) In the original code, the bracket after first_lst ) is misplaced. If we place that bracket after the sec [0] instead of list comprehension we get generator expression.

How do you get a value from a generator in Python?

next () and __next__ () with Generators in Python In the previous code example, we can see that calling a generator creates a generator object rather than getting the required value. To get values from a generator, we can use either next () or __next__ () on the resultant generator object. Example of next () in Python


1 Answers

You need to use tuple() or list() to convert that generator expression to a list or tuple:

[tuple((fir[0], fir[1]*sec[1], fir[2]*sec[2]) for fir in first_lst)\
                               for sec in second_lst if fir[0] == sec[0]]

Working version of your code:

>>> first_lst = [tuple(float(y) for y in x) for x in first_lst]
>>> second_lst = [tuple(float(y) for y in x) for x in second_lst]

>>> [((fir[0],) + tuple(x*y for x, y in zip(fir[1:], sec[1:]))) \
                  for fir in first_lst for sec in second_lst if fir[0]==sec[0]]
[(-2.5, 0.9359, 1.0555999999999999), (-2.0, 0.9516000000000001, 1.04)]
like image 178
Ashwini Chaudhary Avatar answered Nov 15 '22 21:11

Ashwini Chaudhary