python常用列表(数组)操作演示

2022-04-25 00:00:00 数组 常用 演示

python常用列表(数组)操作演示,包括创建列表,添加新元素,获取列表长度,删除元素,数组翻转,列表排序,在指定位置添加元素,统计某元素出现的次数等等

s = ['h','e','l','l','o']   #create a list
s.append('d')               #append to end of list
len(s)                      #number of items in list
s.pop(2)                    #delete an item in the middle
s.sort()                    #sorting the list
s.reverse()                 #reversing the list
s.extend(['w','o'])         #grow list
s.insert(1,2)               #insert into list
s.remove('d')               #remove first item in list with value e
s.pop()                     #remove last item in the list
s.pop(1)                    #remove indexed value from list
s.count('o')                #search list and return number of instances found
s = range(0,10)             #create a list over range 
s = range(0,10,2)           #create a list over range with start index and increment

相关文章