类型错误:与numpy的数组prblem:不能按类型的“浮动”非INT繁衍序列 [英] TypeError: can't multiply sequence by non-int of type 'float' : prblem with NumPy arrays
问题描述
我是pretty新Python.I知道,当一个人试图乘以分数即浮动字符串出现此错误。就我而言,我无法弄清楚如何能在numpy的浮点阵列是一个字符串。下面是完成code。
I am pretty new to Python.I know this error occurs when one tries to multiply a string with a fraction i.e float. In my case , I can't figure out how can a numpy floating point array be a string. Here's complete the code.
#!usr/bin/python
import os
import numpy as np
import matplotlib.pyplot as plt
import pylab
from numpy import *
# Declaration of constants
K = 3.14159265359
N= 10
p_za=[]
pv_za_temp=[]
#-----------------------
#Allotment of particles
#-----------------------
p_initial = np.linspace(0,2,num=10)
pv_za_temp.append(np.array(p_initial))
print pv_za_temp
#Displacement of particles using Zeldovich Approximation
def t_range(start, end, step):
while start <= end:
yield start
start += step
for t in t_range(0,1,0.1):
print t
p_za=[]
pv_za=[]
# Opening file in file_t format
fname = 'file_' + str(t) + '.dat'
fo = open(fname,'w')
# p_za.append(p_initial - t*K*np.sin(K*p_initial))
pv_za.append(K*np.sin(K*pv_za_temp))
pv_za_temp = []
pv_za_temp.append(np.array(pv_za))
这是显示错误行是:
pv_za.append(K*np.sin(K*pv_za_temp))
我想不通的问题。当此行显示错误,它上面的线出来的作品非常清楚。错误消息是:
I can't figure out the problem. When this line shows error, the line above it works out perfectly well. The error message is :
Traceback (most recent call last):
File "ZA.py", line 40, in <module>
pv_za.append(K*np.sin(K*pv_za_temp))
TypeError: can't multiply sequence by non-int of type 'float'
请帮帮忙!
推荐答案
pv_za_temp
是一个普通的Python列表,它不支持标量乘法。你需要一个numpy的数组,你可以从一个完整的列表通过使用创建
pv_za_temp
is an ordinary Python list, which doesn't support scalar multiplication. You need a NumPy array, which you can create from a complete list by using
pv_za_temp = np.array(pv_za_temp)
注意 .array
不支持 .append
。要么完全建立名单提前,或使用 hstack
或 vstack
为宜。
Note that .array
doesn't support .append
. Either build the list completely in advance, or use hstack
or vstack
as appropriate.
这篇关于类型错误:与numpy的数组prblem:不能按类型的“浮动”非INT繁衍序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!