列表批量修改

>>> del lst
>>> lst = list('hello howareyou')
>>> lst
['h', 'e', 'l', 'l', 'o', ' ', 'h', 'o', 'w', 'a', 'r', 'e', 'y', 'o', 'u']
>>> ''.join(lst)
'hello howareyou'
>>> del lst[3:6]
>>> lst
['h', 'e', 'l', 'h', 'o', 'w', 'a', 'r', 'e', 'y', 'o', 'u']
>>> lst
['h', 'e', 'l', 'h', 'o', 'w', 'a', 'r', 'e', 'y', 'o', 'u']
>>> lst[5:] = list('ABCD')       #   列表批量修改
>>> lst
['h', 'e', 'l', 'h', 'o', 'A', 'B', 'C', 'D']
>>> len(lst)
9
>>> lst[15:20]             # 超索引访问,不会报错
[]
>>> lst
['h', 'e', 'l', 'h', 'o', 'A', 'B', 'C', 'D']

  

posted @ 2023-05-07 23:29  sangern  阅读(12)  评论(0编辑  收藏  举报