繁体   English   中英

得到<generator object <genexpr>

[英]Getting <generator object <genexpr>

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

我想对它做以下数学运算:

0.49乘以1.91 (来自first_lstsecond_lst的相应值),并将0.52乘以2.03 (也有相应的值)。 我想在每个对应元组中位置0处的值是相同的条件下这样做,所以-2.50 == -2.50等等。显然,我们也对元组进行相同的数学运算。

我的代码:

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

然而生成一些对象:

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

你能帮我修复代码吗?

您需要使用tuple()list()将该生成器表达式转换为listtuple

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

代码的工作版本:

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

考虑到您的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')]

以下列表理解可能有用。

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

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

这是您的代码的工作版本:

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

打印(答案)

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

有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')]
>>> [((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
>>>

<generator object <genexpr>消息被提及。

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) 在原代码中, 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>]

在语法方面,唯一的区别是使用括号而不是方括号。

注意:如果需要,有两种方法可以将生成器对象转换为列表:

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) 显式使用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)]
>>>

代替元组或列表,您正在制作生成器

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM