12
votes

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?

5
Your tuples have mismatching types of string and float. Is that intentional or just an error? Your LC would fail if you have these as your tuples. Since float cannot be multiplied to a string. See my answer to fix that error. - Sukrit Kalra
I had written this code before I realized I needed to convert everything to float. Will edit it now not to confuse anyone. Thanks for pointing it out. - nutship
Well Ashwini's answer solves the problem. Generally when you are using list comprehensions, you have to typecast/store the final result into either a list or a tuple. - Sagar_c_k

5 Answers

21
votes

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)]
2
votes

Considering that your first_lst and second_lst are 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 useful.

>>> [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)]
2
votes

i had the same problem and found a simpler answer for your question. only thing you need to do is using original for loop syntax and it works nicely!

this is working version of your code:

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

print(ans)

output=[['-2.50', 0.9359, 1.0555999999999999], ['-2.00', 0.9516000000000001, 1.04]]
0
votes

There are 2 issues to look at.

The original code will generate the 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> message is mentioned.

1) Let's fix the the code with minimum amount of changes by creating 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 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. And that will cause the <generator object <genexpr> message:

>>> [((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 one uses parenthesis instead of square brackets.

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

2a) Use asterisk (*) operator to unpack object to the 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
votes

Instead of tuple or list, you are making the generators

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]] 
output:-[('-2.50', 0.9359, 1.0555999999999999), ('-2.00', 0.9516000000000001, 1.04)]