【问题标题】:What does these error about formating mean?这些关于格式化的错误是什么意思?
【发布时间】:2019-12-08 21:38:02
【问题描述】:

请问这个错误是什么意思? 我有这个代码:

steps = np.loadtxt('par.in', unpack=True, usecols=[4])
maximum = np.loadtxt('par.in', unpack=True, usecols=[3])
minimum = np.loadtxt('par.in', unpack=True, usecols=[2])
a = "%0.2f" % (steps[1])
b = "%0.2f" % (maximum[1])
c = "%0.2f" % (minimum[1])


for i in np.arange (a,b,c):
    x, y, chi2, chi2r = np.loadtxt('omega_'+str("%0.2f" % i), unpack=True, usecols=[0, 2, 3, 4])

错误是:

Traceback(最近一次调用最后一次):文件“program.py”, 第 34 行,在 for i in np.arange (omega_min,omega_max,omega_krok): TypeError: unsupported operand type(s) for -: 'str' and 'str'

【问题讨论】:

  • a,b,c 是字符串而不是 int 类型
  • np.arrange() 期望 3 个 int 参数,你通过 3 str 代替。

标签: python formatting


【解决方案1】:

您将参数传递给np.arange,它们是str 类型,但您需要将参数传递为int/float 类型

steps = np.loadtxt('par.in', unpack=True, usecols=[4])
maximum = np.loadtxt('par.in', unpack=True, usecols=[3])
minimum = np.loadtxt('par.in', unpack=True, usecols=[2])
a = float("%0.2f" % (steps[1]))
b = float("%0.2f" % (maximum[1]))
c = float("%0.2f" % (minimum[1]))


for i in np.arange (a,b,c):
    x, y, chi2, chi2r = np.loadtxt('omega_'+str("%0.2f" % i), unpack=True, usecols=[0, 2, 3, 4])  

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 2013-03-31
    相关资源
    最近更新 更多