# >>> alist= [1,2,3,'bob','alice'] # >>> alist[0] = 10 #列表脚标为0的字符改为10 # >>> alist # [10, 2, 3, 'bob', 'alice'] # >>> alist[1:3] = [20,30] #将列表脚标为1,2(不包括3)的字符改为20和30 # >>> alist # [10, 20, 30, 'bob', 'alice'] # >>> alist[2:2] = [22,24,26,28] #在脚标为2的后面再加字符 # >>> alist # [10, 20, 22, 24, 26, 28, 30, 'bob', 'alice'] # # >>> alist.append(100) #向alist列表中加入字符100 # >>> alist # [10, 20, 22, 24, 26, 28, 30, 'bob', 'alice', 100] # >>> alist.remove(24) #移除alist列表中24这个字符 # >>> alist # [10, 20, 22, 26, 28, 30, 'bob', 'alice', 100] # >>> alist.index('bob') #查看脚标 # 6 # >>> blist = alist.copy() # >>> blist # [10, 20, 22, 26, 28, 30, 'bob', 'alice', 100] # >>> alist.insert(1,15) #向列表的脚标为1的字符后面加入字符15 # >>> alist # [10, 15, 20, 22, 26, 28, 30, 'bob', 'alice', 100] # >>> alist.pop() #默认弹出列表最后一个字符 # 100 # >>> alist # [10, 15, 20, 22, 26, 28, 30, 'bob', 'alice'] # >>> alist.pop(2) #弹出列表的脚标为2的字符 # 20 # >>> alist # [10, 15, 22, 26, 28, 30, 'bob', 'alice'] # >>> alist.pop(alist.index('bob')) #弹出字符... # 'bob' # >>> alist # [10, 15, 22, 26, 28, 30, 'alice'] # >>> alist.sort() #排序,类型不同,所以报错 # Traceback (most recent call last): # File "<stdin>", line 1, in <module> # TypeError: '<' not supported between instances of 'str' and 'int' # >>> alist.reverse() #列表顺序反转 # >>> alist # ['alice', 30, 28, 26, 22, 15, 10] # >>> alist.count(20) #统计20在列表中的个数 # 0 # >>> alist.clear() #清除列表的内容 # >>> alist # [] # >>> alist.append('new') #向列表中追加内容 # >>> alist # ['new'] # >>> alist.extend('new') #单纯的字符串会被拆开 # >>> alist # ['new', 'n', 'e', 'w'] # >>> alist.extend(['hello','word','hehe']) # >>> alist # ['new', 'n', 'e', 'w', 'hello', 'word', 'hehe']
python-列表方法(44)
最新推荐文章于 2022-11-08 09:45:00 发布