[英]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_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>]
你能幫我修復代碼嗎?
您需要使用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)]
考慮到您的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)]
我遇到了同樣的問題,並為您的問題找到了一個更簡單的答案。 您唯一需要做的就是使用原始的 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.