列表常用方法

列表:

name=['who','where','how']

  1.切片:左闭右开

   name[:]=name

   name[0:4]='who'

  2.追加:

   name.append('when')===>['who','where','how','when']

  3.插入

   name.insert(1,'loser')===>['who','loser','where','how']

  4.修改

   name[1]='victor'===>['who','victor','how']

  5.删除

   name.remove(1)===>name=['who','how']

   name.pop(1)===>name=['who','how']

   del name('where')===>name=['who','how']

  6.拓展

   p=['i','he','you','who']

   name.extend(p)===>name=['who','where','how','i','he','you','who']

  7.统计

   name=['who','where','how','i','he','you','who']

   name.count('who')=2

  8.排序、反转(python3 中不同数据类型无法排序)

    name=['who','where','how','i','he','you','who','1','2','3']

    name.reverse()

    name=['3', '2', '1', 'who', 'you', 'he', 'i', 'how', 'where', 'who']

    name.sort()

    name=['1', '2', '3', 'he', 'how', 'i', 'where', 'who', 'who', 'you']

  9.获取下标

    name=['who','where','how']

    name.index('who')=0

  10.拷贝

   name=['who','where','how']

   浅copy:

   a=name.copy()

   a=name[:]

   a=list(name)

     当有嵌套列表时,修改a会将name中嵌套列表的数据修改掉

   name=['who','where','how',['when','here']]

   a=name.copy()

   a[1]='zh',a=['who','a','how',['when','here']],name=['who','where','how',['when','here']]

   当修改嵌套内列表时,a和name都会改变

   name=['who','where','how',['when','here']]

   a[3][1]='there'

   a=['who','where','how',['when','there']]

   name=['who','where','how',['when','there']]

   深copy:

     import copy

     name=['who','where','how',['when','here']]

     a=copy.deepcopy(name) 

     a[3][1]='there'

     name=['who','where','how',['when','here']]

     a=['who','where','how',['when','there']]

   

posted @ 2017-09-20 21:48  zhuh  阅读(174)  评论(0编辑  收藏  举报