14

我有 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_lst0.522.030-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>]

你能帮我修复代码吗?

4

5 回答 5

23

您需要使用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]]

您的代码的工作版本:

>>> 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)]
于 2013-05-04T10:55:49.570 回答
2

考虑到您的first_lstandsecond_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)]
于 2013-05-04T11:07:22.857 回答
2

我遇到了同样的问题,并为您的问题找到了一个更简单的答案。您唯一需要做的就是使用原始的 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]]
于 2020-04-07T13:18:42.737 回答
0

有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)]
>>>
于 2018-01-09T01:30:02.430 回答
0

您正在制作生成器,而不是元组或列表

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)]
于 2021-01-22T07:28:36.013 回答