• 列表常用方法


    列表:

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

       

  • 相关阅读:
    asp.net 读取Excel文档
    ASP.NET实现IE下禁用浏览器后退按钮办法
    asp.net 追加文本(追加写入记事本)
    Sql Server中charindex、patindex的区别
    css3 简单动画
    ie6下兼容问题
    IE6下 input 背景图滚动问题及标签规范
    css2---必须学的经典---定位问题
    EF 用CallContext上下文管理
    EF查询分页
  • 原文地址:https://www.cnblogs.com/zh-20170913/p/7565270.html
Copyright © 2020-2023  润新知