Python 获取 <generator 对象 <genexpr>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16373484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:24:30  来源:igfitidea点击:

Getting <generator object <genexpr>

pythonpython-2.7list-comprehension

提问by nutship

I have 2 lists:

我有 2 个列表:

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.49by 1.91(the corresponding values from first_lstand second_lst), and multiply 0.52by 2.03(corresponding values also). I want to do that under condition that values at position 0in each corresponding tuple is idential so -2.50== -2.50etc. Obviously, we do the same math for remaning tuples as well.

乘法0.491.91(从相应的值first_lstsecond_lst),和乘法0.522.03(也对应值)。我想0在每个对应元组中位置的值是相同的情况下这样做,所以-2.50==-2.50等。显然,我们也对元组进行相同的数学运算。

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?

你能帮我修复代码吗?

采纳答案by Ashwini Chaudhary

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

您需要使用tuple()orlist()将该生成器表达式转换为 a listor 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)]

回答by Sukrit Kalra

Considering that your first_lstand second_lstare defined as follows.

考虑到您的first_lstsecond_lst定义如下。

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

回答by sg7

There are 2 issues to look at.

有2个问题要看。

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.

<generator object <genexpr>提到了消息。

1) Let's fix the the code with minimum amount of changes by creating list comprehension:

1)让我们通过创建列表理解来以最少的更改修复代码:

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

2)在原代码中,后面的括号first_lst)错位了。如果我们将该括号放在sec[0]而不是列表推导式之后,我们将得到生成器表达式。这将导致<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 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

2a) 使用星号 (*) 运算符将对象解包到列表中

>>> [*((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()

2b) 明确使用 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)]
>>>

回答by MaYSaM

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!

我遇到了同样的问题,并为您的问题找到了一个更简单的答案。您唯一需要做的就是使用原始的 for 循环语法,它运行良好!

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