您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python开发_python中的list操作

5b51 2022/1/14 8:24:16 python 字数 56038 阅读 664 来源 www.jb51.cc/python

python开发_python中的list操作

概述

对python中list的操作,大家可以参考:

效果:

下面有更多的demo:

<div class="cnblogs_code">

 Python 3.3.2 (v3.3.2:d047928ae3f6,00:03:43) [MSC v.1600 32 Type ,    >>> counter = 100
 >>> miles = 1000.0
 >>> name = 
 >>> numberA,numberB,nameC = 1,
 >>> list = >>>  [100,1000.0,, >>> 
 >>>  element       
 100
 1000.0
  1
 2
  >>> 
 >>> (list[0]) 获取列表list里面的第一个元素值
 100
 >>> (list[-1]) 获取列表list里面的最后一个元素值
  >>> (len(list)) 获取list列表的长度
 6
 >>> num_inc_list = range(10) 一个数值递增的列表
 >>>  range(0,10 >>>  inc_list       
  1
 2
 3
 4
 5
 6
 7
 8
 9
 >>> 一个从0开始到9的一个数值递增列表
 >>> initial_value = 10
 >>> list_length = 5
 >>> myList = [initial_value  i  range(10 >>>  [10,10,10 >>> list_length = 2
 >>> myList = myList * >>>  [10,10 >>>  20
 >>> 一个固定值initial_value去初始化一个列表myList
 >>> 
 >>> 效果
 >>> copyList = [1, >>> copyList = copyList * >>>  8
 >>>  cl       
     
 1
 2
 3
  1
 2
 3
  >>> 
 >>> 一个list中可以包含不同类型的元素,这个和ActionScript 3.0(AS3.0)中的数组类似
 >>> test_list = [,, >>>  7
 >>> (test_list[0]) 
  >>> 
 >>> (test_list[-1]) 一个元素
  >>> (test_list[-len]) 一个元素
    File ,line 1, 
                                                                                                                                                                                                             

                                                                                                                                                                                                                   (test_list[-len]) 一个元素
 TypeError: bad operand type  unary -: 
 >>> (test_list[-len(test_list)]) 一个元素
  >>> (test_list[len(test_list) - 1]) 一个元素
  >>> test_list.append(6) 一个元素
 >>> (test_list[-1 6
 >>> test_list.insert(1 >>>  [,,6 >>> 
 >>> test_list.insert(1 >>>  [,6 >>> test_list.insert(2,1 >>>  [,6 >>> (test_list.pop(0)) 一个元素,并从test_list中删除
  >>>  [0,6 >>> (test_list.pop(2)) 错误,pop(index)的操作是返回数组下标为index的元素,并从列表中删除
  >>>  [0,6 >>> test_list.remove(1 >>>  [0,6 >>> 删除第一次出现的元素1
 >>> test_list.insert(0,1 >>>  [1,6 >>> test_list.remove(1 >>>  [0,6 >>> test_list.insert(2, >>>  [0,6 >>> test_list.count( 2
 >>> 统计var元素在列表中出现的个数
 >>> test_list.count(  >>> test_list_extend = [,, >>> >>>  [0, >>> 一个list到源list上面
 >>> .sort())
    File , 
                                                                                                                                                                                                                                                                                                                                                                             

                                                                                                                                                                                                                                                                                                                                                                                   .sort())
 TypeError: unorderable types: str() < >>> test_list_extend.append( >>> test_lsit_extend.append(    File , 
                                                                                                                                                                                                                                                                                                                                                                                                  

                                                                                                                                                                                                                                                                                                                                                                                                        test_lsit_extend.append( NameError: name    >>> list_a = [,,, >>> >>>  [,, >>> (list_a.sort()) 
  >>> 
 >>> list_b = [1,4 >>> .sort())
  >>>     File , 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                     

                                                                                                                                                                                                                                                                                                                                                                                                                                                                            NameError: name    >>> 删除操作吧!!!!!
 >>>  [1,6 >>> ( list_b[1 SyntaxError: invalid Syntax
 >>>  list_b[1 >>>  [1,6 >>>  list_b[0,2    File , 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    list_b[0,2 TypeError: list indices must be integers, >>>  list_b[0:2 >>>  [4,6 
 >>> 删除下标为index的元素,del list[start:end]删除从start下标开始到end下标结束的元素
 >>>  list_b[10    File , 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               list_b[10  >>> 删除的下标超出了列表的长度范围,就会报错啦!!!!!
 >>> 
 >>> list_c = range(5 >>>  c       
     
  1
 2
 3
 4
 >>> list_d = >>>  d       
     
  1
 2
 3
 4
 >>> 
 >>> list_d[2] = 23
    File , 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     list_d[2] = 23
 TypeError:  object does  >>> list_e = [1,5 >>> list_f = >>> list_f[2] = 234
 >>>  [1,234,5 >>> 一个引用,
 >>> 一个对象:[1,5],当我们视图修改list_f[2]的值的时候,
 >>> 
 >>> 
 >>> 
 >>> list_i = >>>  [1,5 >>>  [1,5 >>> list_i[2] = 3
 >>>  [1,5 >>>  [1,5 >>> 一个列表,这样的操作,会创造出新的一个列表对象
 >>> 
 >>> 
 >>> 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                             


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶