Getting the <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:

Multiply 0.49 by 1.91 (the corresponding values ​​from first_lst and second_lst ) and multiply 0.52 by 2.03 (the corresponding values ​​also). I want to do this provided that the values ​​at position 0 in each corresponding tuple are identical, so -2.50 == -2.50 , etc. Obviously, we are doing the same math to restore tuples.

My code is:

 [((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]] 

Creates, however, some object:

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

Can you help me fix the code?

+4
source share
3 answers

You need to use tuple() or list() to convert this generator expression to 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]] 

The working version of your code is:

 >>> 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)] 
+8
source

Given that your first_lst and second_lst defined as follows.

 >>> 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')] 

The following list comprehension may be helpful.

 >>> [tuple((float(elem[0][0]), float(elem[0][1])*float(elem[1][1]), float(elem[0][2])*float(elem[1][2]))) for elem in zip(first_lst, second_lst) if elem[0][0]==elem[1][0]] [(-2.5, 0.9359, 1.0555999999999999), (-2.0, 0.9516000000000001, 1.04)] 
+1
source

There are two issues that need attention.

The source code generates an error:

 >>> 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')] >>> [((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]] Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 1, in <listcomp> NameError: name 'fir' is not defined >>> 

and <generator object <genexpr> .

1). Correct the code with as few changes as possible by creating a list comprehension :

 >>> 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')] >>> [(fir[0],fir[1]*float(sec[1]),fir[2]*float(sec[2])) for fir in first_lst for sec in second_lst if fir[0] == sec[0]] # list comprehension [('-2.50', 0.9359, 1.0555999999999999), ('-2.00', 0.9516000000000001, 1.04)] >>> 

2) In the source code, the bracket after first_lst ) inappropriate. If we place this bracket after sec[0] instead of understanding the list, we get an expression. And this will call the <generator object <genexpr> :

 >>> [((fir[0],fir[1]*float(sec[1]),fir[2]*float(sec[2])) for fir in first_lst for sec in second_lst if fir[0] == sec[0])] # generator object [<generator object <genexpr> at 0x00000184EEDE29E8>] 

In terms of syntax, the only difference is that brackets are used instead of square brackets.

Note. If necessary, there are two ways to convert the generator object to a list:

2a) Use the asterisk operator (*) to unpack an object into a list

 >>> [*((fir[0],fir[1]*float(sec[1]),fir[2]*float(sec[2])) for fir in first_lst for sec in second_lst if fir[0] == sec[0])] [('-2.50', 0.9359, 1.0555999999999999), ('-2.00', 0.9516000000000001, 1.04)] >>> 

2b) Use explicitly list()

 >>> list((fir[0],fir[1]*float(sec[1]),fir[2]*float(sec[2])) for fir in first_lst for sec in second_lst if fir[0] == sec[0]) [('-2.50', 0.9359, 1.0555999999999999), ('-2.00', 0.9516000000000001, 1.04)] >>> 
0
source

Source: https://habr.com/ru/post/1479121/


All Articles