类型错误:用于/:'float'和'list'的不受支持的操作数类型

时间:2022-09-10 22:06:24

I tried to calculate element-wise dividing for two list:

我试着计算两张表的元素划分:

outCount = [0,2,1,3]
PR_old = [0.25,0.25,0.25,0.25]
inSum = 0
for j in range(len(outCount)):
    if outCount[j] == 0:
        continue
    inSum += PR_old[j]/outCount[j]

And I get the following error:

我得到了以下错误:

TypeError: unsupported operand type(s) for /: 'float' and 'list'

2 个解决方案

#1


3  

Apart from the incorrect indentation of the for loop, your code is fine. It should be like this:

除了for循环的缩进错误外,您的代码没有问题。应该是这样的:

outCount = [0,2,1,3]
PR_old = [0.25,0.25,0.25,0.25]
inSum = 0
for j in range(len(outCount)):
    if outCount[j] == 0:
        continue
    inSum += PR_old[j]/outCount[j]

print inSum

However, there are more Pythonic ways to do this. You don't really need that index variable j. You can iterate over both lists simultaneously using zip():

然而,有更多的勾股定理。您并不需要索引变量j。您可以使用zip()同时遍历两个列表:

inSum = 0
for u, v in zip(PR_old, outCount):
    if v:
        inSum += u / v

Or doing essentially the same thing in a generator expression and using the built-in sum() function to perform the addition:

或者在生成器表达式中执行相同的操作,并使用内置sum()函数执行加法:

inSum = sum(u / v for u, v in zip(PR_old, outCount) if v)

FWIW, the final value of inSum is 0.458333333333

FWIW, inSum的最终值是0.45833333333333

(Code tested on Python 2.6.6)

(在Python 2.6.6上测试的代码)

#2


0  

Apart from your obvious indentation error, I can't reproduce your problem. Specifically:

除了你明显的缩进错误,我无法重现你的问题。具体地说:

2>>> outCount = [0,2,1,3]
2>>> PR_old = [0.25,0.25,0.25,0.25]
2>>> inSum = 0
2>>> for j in range(len(outCount)):
2...         if outCount[j] == 0:
2...             continue
2...         inSum += PR_old[j]/outCount[j]
2... 
2>>> print(inSum)
0.458333333333

(the 2 in front of the prompts is just to show I'm using Python 2, but it doesn't matter, it would work the same in Python 3).

(提示符前面的2只是显示我使用的是Python 2,但没关系,在Python 3中也是一样的)。

Please copy and paste this code (editing the prompts away of course) into a Python interactive interpreter, and show us where, exactly, in this code you show us, you get the error you report.

请将此代码(当然是编辑提示符)复制并粘贴到Python交互式解释器中,并在您向我们展示的代码中显示您报告的错误。

#1


3  

Apart from the incorrect indentation of the for loop, your code is fine. It should be like this:

除了for循环的缩进错误外,您的代码没有问题。应该是这样的:

outCount = [0,2,1,3]
PR_old = [0.25,0.25,0.25,0.25]
inSum = 0
for j in range(len(outCount)):
    if outCount[j] == 0:
        continue
    inSum += PR_old[j]/outCount[j]

print inSum

However, there are more Pythonic ways to do this. You don't really need that index variable j. You can iterate over both lists simultaneously using zip():

然而,有更多的勾股定理。您并不需要索引变量j。您可以使用zip()同时遍历两个列表:

inSum = 0
for u, v in zip(PR_old, outCount):
    if v:
        inSum += u / v

Or doing essentially the same thing in a generator expression and using the built-in sum() function to perform the addition:

或者在生成器表达式中执行相同的操作,并使用内置sum()函数执行加法:

inSum = sum(u / v for u, v in zip(PR_old, outCount) if v)

FWIW, the final value of inSum is 0.458333333333

FWIW, inSum的最终值是0.45833333333333

(Code tested on Python 2.6.6)

(在Python 2.6.6上测试的代码)

#2


0  

Apart from your obvious indentation error, I can't reproduce your problem. Specifically:

除了你明显的缩进错误,我无法重现你的问题。具体地说:

2>>> outCount = [0,2,1,3]
2>>> PR_old = [0.25,0.25,0.25,0.25]
2>>> inSum = 0
2>>> for j in range(len(outCount)):
2...         if outCount[j] == 0:
2...             continue
2...         inSum += PR_old[j]/outCount[j]
2... 
2>>> print(inSum)
0.458333333333

(the 2 in front of the prompts is just to show I'm using Python 2, but it doesn't matter, it would work the same in Python 3).

(提示符前面的2只是显示我使用的是Python 2,但没关系,在Python 3中也是一样的)。

Please copy and paste this code (editing the prompts away of course) into a Python interactive interpreter, and show us where, exactly, in this code you show us, you get the error you report.

请将此代码(当然是编辑提示符)复制并粘贴到Python交互式解释器中,并在您向我们展示的代码中显示您报告的错误。