【问题标题】:python float object does not support item assignmentpython浮动对象不支持项目分配
【发布时间】:2018-03-13 05:20:43
【问题描述】:

我正在运行以下代码,但收到错误消息“浮动”对象不支持项目分配。我想要的输出是将这些计算的结果附加到向量 Vi 中

import numpy as np

MolWeight = [132, 320, 29, 45, 10]
Ci = 10 # g/L initial Concentration
Cf = 50*10**(-6) #M final Concentration
Vf = 100*10**(-6) #Litre final Volume
Vi = []
for i in range(len(MolWeight)):
    #How many moles of the compounds are there in the standard solution
    Mi = Ci/MolWeight[i] #M
    #this corresponds to the initial concentration of the standard     compound
    Ci = Mi #M/L
    #I calculate the volume to extract from the standard compound    solution, so to obtain the desired concentration 
    np.append.Vi[i] = (Cf*Vf)/Ci #L

【问题讨论】:

  • @Evert 附加大括号错误Vi[i].append(Cf*Vf/Ci)。但正确的建议。

标签: python object variable-assignment


【解决方案1】:

这就是你要找的吗? 我认为问题是 int 除法和追加

import numpy as np

MolWeight = [132.0, 320.0, 29.0, 45.0, 10.0]
Ci = 10.0 # g/L initial Concentration
Cf = 50*10**(-6) #M final Concentration
Vf = 100*10**(-6) #Litre final Volume
Vi = []
for i in range(len(MolWeight)):
    #How many moles of the compounds are there in the standard solution
    Mi = Ci/MolWeight[i] #M
    #this corresponds to the initial concentration of the standard     compound
    Ci = Mi  # M/L
    #I calculate the volume to extract from the standard compound    solution, so to obtain the desired concentration 
    # np.append.Vi[i] = (Cf * Vf) / Ci  # L
    Vi.append((Cf * Vf) / Ci )


print Vi

输出

 [6.599999999999998e-08, 2.1119999999999998e-05, 0.0006124799999999999, 0.027561599999999995, 0.27561599999999997]

【讨论】:

    【解决方案2】:

    你可以这样做

    Vi.append((Cf*Vf)/Ci) #L
    

    输出:

    Vi
    [6.599999999999998e-08,
     2.1119999999999998e-05,
     0.0006124799999999999,
     0.027561599999999995,
     0.27561599999999997]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 2016-10-12
      • 1970-01-01
      相关资源
      最近更新 更多