我有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_lst
和second_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>]
你能帮我解决一下代码吗?
答案 0 :(得分:14)
您需要使用tuple()
或list()
将该生成器表达式转换为list
或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)]
答案 1 :(得分:1)
考虑到您的first_lst
和second_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)]
答案 2 :(得分:1)
我遇到了同样的问题,并为您的问题找到了一个更简单的答案。 唯一需要做的就是使用原始的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])])
打印(ans)
output=[['-2.50', 0.9359, 1.0555999999999999], ['-2.00', 0.9516000000000001, 1.04]]
答案 3 :(得分: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], 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)通过创建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)在原始代码中,first_lst
)
之后的方括号错位。
如果我们在sec[0]
而不是列表理解之后放置该括号,我们会得到generator expression。这将导致<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)]
>>>
答案 4 :(得分: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)]