首页 > 其他 > 详细

列表常用方法

时间:2017-09-20 22:53:12      阅读:258      评论:0      收藏:0      [点我收藏+]

列表:

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‘]]

   

列表常用方法

原文:http://www.cnblogs.com/zh-20170913/p/7565270.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!