草庐IT

str_index

全部标签

python - 'str' 对象没有属性 '__dict__'

我想在Python中将字典序列化为JSON。我有这个'str'objecthasnoattribute'dict'错误。这是我的代码...fromdjango.utilsimportsimplejsonclassPerson(object):a=""person1=Person()person1.a="111"person2=Person()person2.a="222"list={}list["first"]=person1list["second"]=person2s=simplejson.dumps([p.__dict__forpinlist])异常(exception)情况是;

Python Force List Index out of Range 异常

我有一个列表列表x=[[1,2,3],[4,5,6],[7,8,9]]我希望代码抛出数组越界异常,类似于索引超出范围时在Java中的做法。例如,x[0][0]#1x[0][1]#2x[0-1][0-1]#如果抛出异常,我希望它返回0。try:x[0-1][0-1]#Iwantthistothrowanexceptionexcept:print0#printstheinteger0我认为基本上只要索引为负,就抛出异常。 最佳答案 您可以创建自己的列表类,继承默认列表类,并实现返回指定索引中元素的__getitem__方法:classM

Python 继承 : Concatenating with super __str__

我想将子类的__str__实现添加到基础实现中:classA:def__str__(self):return"this"classB(A):def__str__(self):returnsuper(B,self)+"+that"但是,这会产生类型错误:TypeError:unsupportedoperandtype(s)for+:'super'and'str'有没有办法让str(B())返回"this+that"? 最佳答案 你需要做super(B,self).__str__()。super指的是父类;您没有调用任何方法。

python - str() 在 Python 中会失败吗?

在Python中是否存在str()抛出异常的情况? 最佳答案 是的,自定义类可能会失败:>>>classC(object):...def__str__(self):...return'oops:'+oops...>>>c=C()>>>str(c)NameError:globalname'oops'isnotdefined对于某些内置类,它甚至可能会失败,例如unicode:>>>u=u'\xff'>>>s=str(u)UnicodeEncodeError:'ascii'codeccan'tencodecharacteru'\xff'

python - Pandas.DataFrame.rename 方法中的参数 "index"是什么?

PandasDataFrame有一个重命名方法,它接受一个名为“index”的参数。看不懂文档中对参数的描述:DataFrame.rename具体来说,我像文档网页上的示例一样使用它:df.rename(index=str,columns={"A":"a","B":"c"})我理解结果,但我不明白为什么我们设置index=str。index参数有什么用?为什么示例设置index=str? 最佳答案 index参数用于重命名索引,以df为例:df.index#RangeIndex(start=0,stop=3,step=1)df.re

python - 索引错误 : boolean index did not match indexed array along dimension 0

在我将Numpy更新到1.13.1之前,我的代码工作正常。现在我得到以下错误IndexError:booleanindexdidnotmatchindexedarrayalongdimension0;dimensionis5butcorrespondingbooleandimensionis4...在这一行抛出:m=arr[np.diff(np.cumsum(arr)>=sum(arr)*i)]我似乎无法理解它。有什么建议吗?这是我的示例代码:a=[1,2,3,4,5]l=[0.85,0.90]s=sorted(a,reverse=False)arr=np.array(s)foriin

【Python报错-02】解决Python中的join()函数报错 :sequence item 0: expected str instance, int found

1报错内容:TypeError:sequenceitem0:expectedstrinstance,intfound。TypeError:序列项0:应为str实例,但找到list。原代码如下:str1='\n'f=open('labels.txt','w')f.write(str1.join(labels)) #这句话报错f.close()2了解join()函数语法:str.join(sequence)参数:可连接对象:列表,元组,字符串,字典和集合(都得是字符串)#参数#sequence-要连接的元素序列。比如:列表,元组,字符串,字典和集合#str-以什么来连接元素3解决办法(1)根据错

python - 如何将函数转换为 str for Python?

假设我有以下lambda函数。fn=lambdax:print(x)如果我想把它转换成字符串"lambdax:print(x)"我能做什么?我期待str(fn)或str(fn.__code__)会这样做,但不是真的......它只是打印出类型、内存位置等。我也尝试过pickle.dumps和json,但我无法得到我想要的。如何将函数转换为显示函数定义的字符串?---我想将函数作为输入并将其转换为字符串 最佳答案 如果你已经安装了dill就很容易了。(pipinstalldill)fromdill.sourceimportgetsou

python - 值错误 : DataFrame index must be unique for orient ='columns'

我将许多数据框合并成一个更大的数据框,pd.concat(dfs,axis=0)然后我可以不将它转储到json(Pdb)df.to_json()***ValueError:DataFrameindexmustbeuniquefororient='columns'.我该如何解决? 最佳答案 该错误表明您的数据帧索引具有非唯一(重复)值。由于您似乎没有使用索引,因此您可以创建一个新索引:df.reset_index(inplace=True)或df.reset_index(drop=True,inplace=True)如果你想删除之前的

python - 可见弃用警告 : boolean index did not match indexed array along dimension 1; dimension is 2 but corresponding boolean dimension is 1

Macports更新后,我认为更新了numpy,我收到警告:VisibleDeprecationWarning:booleanindexdidnotmatchindexedarrayalongdimension1;dimensionis2butcorrespondingbooleandimensionis1inliers=n.size(pixels[distances以前没有提出过。相关代码为:#Computedistanceofallnon-zeropointsfromthecircumferencedistances=guess_feature.points_distance(pi