Python数据类型详解(二)列表
一.基本数据类型
整数:int
字符串:str(注:\t等于一个tab键)
布尔值:bool
列表:list(元素的集合)
列表用[]
元祖:tuple
元祖用()
字典:dict
注:所有的数据类型都存在想对应的类列里
二.列表所有数据类型:
基本操作:
索引,切片,追加,删除,长度,切片,循环,包含
list
classlist(object): """ list()->newemptylist list(iterable)->newlistinitializedfromiterable'sitems """ defappend(self,p_object):#realsignatureunknown;restoredfrom__doc__ """L.append(object)->None--appendobjecttoend""" (L.append(对象)->——没有一个对象附加到结束) pass defclear(self):#realsignatureunknown;restoredfrom__doc__ """L.clear()->None--removeallitemsfromL""" (L.clear()->没有,把所有项目从L) pass defcopy(self):#realsignatureunknown;restoredfrom__doc__ """L.copy()->list--ashallowcopyofL""" (L.copy()->列表-L的浅拷贝) return[] defcount(self,value):#realsignatureunknown;restoredfrom__doc__ """L.count(value)->integer--returnnumberofoccurrencesofvalue""" (L.count(价值)->整数,返回值的出现次数) return0 defextend(self,iterable):#realsignatureunknown;restoredfrom__doc__ """L.extend(iterable)->None--extendlistbyappendingelementsfromtheiterable""" (L.extend(iterable)->没有——从iterable扩展列表通过添加元) pass defindex(self,value,start=None,stop=None):#realsignatureunknown;restoredfrom__doc__ """ L.index(value,[start,[stop]])->integer--returnfirstindexofvalue. RaisesValueErrorifthevalueisnotpresent. (l指数(价值,[开始,[不要]])->整数,返回第一索引值。提出了ValueError如果不存在的价值。) """ return0 definsert(self,index,p_object):#realsignatureunknown;restoredfrom__doc__ """L.insert(index,object)--insertobjectbeforeindex""" (l插入(指数(对象)——前插入对象索引) pass defpop(self,index=None):#realsignatureunknown;restoredfrom__doc__ """ L.pop([index])->item--removeandreturnitematindex(defaultlast). RaisesIndexErroriflistisemptyorindexisoutofrange. (L.pop((指数))->项目——删除并返回项指数(默认)。提出了IndexError如果列表为空或索引的范围。) """ pass defremove(self,value):#realsignatureunknown;restoredfrom__doc__ """ L.remove(value)->None--removefirstoccurrenceofvalue. RaisesValueErrorifthevalueisnotpresent. """ (L.remove(价值)->没有,删除第一次出现的值。提出了ValueError如果不存在的价值。) pass defreverse(self):#realsignatureunknown;restoredfrom__doc__ """L.reverse()--reverse*INPLACE*""" pass defsort(self,key=None,reverse=False):#realsignatureunknown;restoredfrom__doc__ """L.sort(key=None,reverse=False)->None--stablesort*INPLACE*""" pass def__add__(self,*args,**kwargs):#realsignatureunknown """Returnself+value.""" pass def__contains__(self,*args,**kwargs):#realsignatureunknown """Returnkeyinself.""" pass def__delitem__(self,*args,**kwargs):#realsignatureunknown """Deleteself[key].""" pass def__eq__(self,*args,**kwargs):#realsignatureunknown """Returnself==value.""" pass def__getattribute__(self,*args,**kwargs):#realsignatureunknown """Returngetattr(self,name).""" pass def__getitem__(self,y):#realsignatureunknown;restoredfrom__doc__ """x.__getitem__(y)<==>x[y]""" pass def__ge__(self,*args,**kwargs):#realsignatureunknown """Returnself>=value.""" pass def__gt__(self,*args,**kwargs):#realsignatureunknown """Returnself>value.""" pass def__iadd__(self,*args,**kwargs):#realsignatureunknown """Implementself+=value.""" pass def__imul__(self,*args,**kwargs):#realsignatureunknown """Implementself*=value.""" pass def__init__(self,seq=()):#knownspecialcaseoflist.__init__ """ list()->newemptylist list(iterable)->newlistinitializedfromiterable'sitems #(copiedfromclassdoc) """ pass def__iter__(self,*args,**kwargs):#realsignatureunknown """Implementiter(self).""" pass def__len__(self,*args,**kwargs):#realsignatureunknown """Returnlen(self).""" pass def__le__(self,*args,**kwargs):#realsignatureunknown """Returnself<=value.""" pass def__lt__(self,*args,**kwargs):#realsignatureunknown """Returnself<value.""" pass def__mul__(self,*args,**kwargs):#realsignatureunknown """Returnself*value.n""" pass @staticmethod#knowncaseof__new__ def__new__(*args,**kwargs):#realsignatureunknown """Createandreturnanewobject.Seehelp(type)foraccuratesignature.""" pass def__ne__(self,*args,**kwargs):#realsignatureunknown """Returnself!=value.""" pass def__repr__(self,*args,**kwargs):#realsignatureunknown """Returnrepr(self).""" pass def__reversed__(self):#realsignatureunknown;restoredfrom__doc__ """L.__reversed__()--returnareverseiteratoroverthelist""" pass def__rmul__(self,*args,**kwargs):#realsignatureunknown """Returnself*value.""" pass def__setitem__(self,*args,**kwargs):#realsignatureunknown """Setself[key]tovalue.""" pass def__sizeof__(self):#realsignatureunknown;restoredfrom__doc__ """L.__sizeof__()--sizeofLinmemory,inbytes""" pass __hash__=None
三.所有列表数据类型举例
#!/usr/bin/envpython #-*-coding:utf-8-*- #append追加 name_list=["zhangyanlin","suoning","nick"] name_list.append('zhang') print(name_list) #count制定字符出现几次 name_list=["zhangyanlin","suoning","nick"] name_list.append('zhang') name_list.append('zhang') name_list.append('zhang') print(name_list.count('zhang')) #extend可扩展,批量往里加数据 name_list=["zhangyanlin","suoning","nick"] name=["aylin","zhang","yan","lin"] name_list.extend(name) print(name_list) #index找到字符所在的位置 name_list=["zhangyanlin","suoning","nick"] print(name_list.index('nick')) #insert插入,往索引里面插入值 name_list=["zhangyanlin","suoning","nick"] name_list.insert(1,"zhang") print(name_list) #pop在原列表中移除掉最后一个元素,并赋值给另一个变量 name_list=["zhangyanlin","suoning","nick"] name=name_list.pop() print(name) #remove移除,只移除从左边找到的第一个 name_list=["zhangyanlin","suoning","nick"] name_list.remove('nick') print(name_list) #reverse反转 name_list=["zhangyanlin","suoning","nick"] name_list.reverse() print(name_list) #del删除其中元素,删除1到3之间的 name_list=["zhangyanlin","suoning","nick"] delname_list[1:3] print(name_list)
四.索引
name_list=["zhangyanlin","suoning""aylin""nick"] print(name_list[0])
五.切片
name_list=["zhangyanlin","suoning""aylin""nick"] print(name_list[0:2])
六.总长度len
name_list=["zhangyanlin","suoning""aylin""nick"] print(name_list[1:len(name_list)])
七.for循环
name_list=["zhangyanlin","suoning""aylin""nick"] foriinname_list: print(i)