2021
03-10
03-10
Python 实现list,tuple,str和dict之间的相互转换
1、字典(dict)dict={‘name':‘Zara',‘age':7,‘class':‘First'}1.1字典——字符串返回:printtype(str(dict)),str(dict)1.2字典——元组返回:(‘age',‘name',‘class')printtuple(dict)1.3字典——元组返回:(7,‘Zara',‘First')printtuple(dict.values())1.4字典——列表返回:[‘age',‘name',‘class']printlist(dict)1.5字典——列表printdict.values2、元组tup=(1,2,3,4,5)2.1元组——...
继续阅读 >
如下所示:报错原因是传入的是类对象,可你传进的参数是字符串,找到传参的位置改过来即可补充知识:'dict'objecthasnoattribute'has_key'解决办法最近开始学习Python,安装上最新的Python3.6.5在使用django的时候出现如下错误‘dict'objecthasnoattribute'has_key'保留犯罪现场:犯罪现场2:查阅资料发现,Python3以后删除了has_key()方法解决办法:修改代码ifdict.has_key(key1):改为ifkey1inadict:最终修改...
TypeError:cannotconcatenate'str'and'int'objectsprintstr+int的时候就会这样了python+作为连接符的时候,不会自动给你把int转换成str补充知识:TypeError:cannotconcatenate'str'and'list'objects和Python读取和保存图片运行程序时报错,然后我将list转化为str就好了。利用''.join(list)如果需要用逗号隔开,如1,2,3,4则使用','.join(list)Python中plt可以显示和保存图片,不能使用mpingimportmatplotlib.ima...
如下所示:#!/usr/bin/pythonimportpickleshoplist=['apple','mango','carrot']f=open('c:\poem.txt','w')pickle.dump(shoplist,f)f.close()delshoplistf=open('c:\poem.txt','r')storedlist=pickle.load(f)print(storedlist)执行上述程序时候报错:TypeError:mustbestr,notbytes解决方法:在使用open打开文件的时候,加个bf=open('c:\poem.txt','wb‘)f=open('c:\poem.txt','rb')补充知识:TypeError:LoadLibr...