Python list-related how-to
NameList = [' A ', ' B ', ' C ', ' d ', ' 1 ', ' 2 ', ' 3 ', ' 4 ']
Namelist1 = [' A ', ' B ', ' C ', ' d ', ' 1 ', ' 2 ', ' 3 ', ' 4 ']
Print (NameList)
Print (namelist[1]) #取得第2个
Print (namelist[-1]) #取得最后一个 index starting from the last -1,-2,-3
Print (Namelist[0:3]) #取得列表范围 0 to 2 3 values do not contain a value on the 3 sequence
Print (Namelist[-4:][2:4]) #在前一个切片的结果上在进行切片
Namelist.insert (2, ' ee ') #插入
Print (NameList) namelist.append (' a ') #在最后面添加
Print (NameList) namelist.remove (' ee ') #删除
Print (NameList) namelist[1]= "BBB" #编辑元素
Print (namelist) print (Namelist[::2]) #设置 step by one person
Print (' A ' in NameList) # Determines whether the current element is in the list
Print (Namelist.count (' A ')) # Number of current elements in the list
Print (Namelist.index (' 3 ')) #取得 current element index
Namelist.extend (Namelist1) #合并两个列表
Print (NameList) namelist.reverse () #列表反转
Print (NameList) namelist.sort () #排序
Print (NameList) Namelist.pop (2) #用下标删除 default last
Print (NameList)
Newnamelist = namelist.copy () #如果元素中有列表 just the address of the reference element list
Print (newnamelist)
Import Copy #导入Copy库
Newnamelist2 = Copy.deepcopy (namelist) a list of #完全克隆的copy elements is also fully duplicated
Print (len (namelist)) #取得列表长度
Python Basic Learning 3-list usage