Python基础学习-列表基本操作

   列表:Python的“苦力”。

     列表不同于元组和字条串的地方:列表是可变的——可以改变列表的内容,并且列表有很多有用的、专门的方法。

  1、list函数

  因为字符串不能像列表一样被修改,所有有时根据字符创建列表会很有用。list函数可以实现这相操作,

  关于list()函数应用代码示例:

>>> a=list() #不带参数进行调用时将返回一个空列表
>>> a
[]
>>> a=[]     #空列表也可以使用空的方括号来创建
>>> a
[]
>>> b=['H','e','l','l','o',['1','2','3']]
>>> b
['H', 'e', 'l', 'l', 'o', ['1', '2', '3']]
>>> list(b)  #list函数带一个列表参数时,返回该参数的浅拷贝;
['H', 'e', 'l', 'l', 'o', ['1', '2', '3']]
>>> c='No pain,No gain!'
>>> list(c)  #list()对任意其他参数则尝试将给定的对象转换为列表。
['N', 'o', ' ', 'p', 'a', 'i', 'n', ',', 'N', 'o', ' ', 'g', 'a', 'i', 'n', '!']
>>> 

   列表也可以不使用list()函数创建,空列表可以使用空的方括号创建,包含一个或多个项的列表则可以使用逗号分隔的数据项(包含在[]中)序列来创建 。

  代码示例:

>>> list=['zhang3','li4','wang5','马*腾']
>>> list
['zhang3', 'li4', 'wang5', '马*腾']
>>> list=[]
>>> list
[]

   2、列表的基本操作

    2.1 列表取值与赋值

    由于列表中所有数据项实际上都是对象引用,因此,与元组一样,列表也可以存放任意数据类型的数据项,包括组合数据类型,比如列表与元组;

    示例:

>>> L=[-99,'name',55,'a',['ram','2','echo'],234]
>>> L
[-99, 'name', 55, 'a', ['ram', '2', 'echo'], 234]
>>> L[0]
-99
>>> L[-6]
-99
>>> L[1]
'name'
>>> L[-5]
'name'
>>> L[1][0]
'n'
>>> L[-5][0]
'n'
>>> #L[4][2]==L[4][-1]==L[-2][2]==L[-2][-1]
>>> L[4][2]
'echo'
>>> L[-2][-1]
'echo'
>>> 

  列表赋值示例:

>>> L
[-99, 'name', 55, 'a', ['ram', '2', 'echo'], 234]
>>> L[-1]=432
>>> L
[-99, 'name', 55, 'a', ['ram', '2', 'echo'], 432]
>>> L[0]=-17.5 #改变列表索引为0的元素值
>>> L
[-17.5, 'name', 55, 'a', ['ram', '2', 'echo'], 432]
>>> L[4][2][1]='E'
Traceback (most recent call last):
  File "<pyshell#76>", line 1, in <module>
    L[4][2][1]='E'
TypeError: 'str' object does not support item assignment #注意字符串是不能改变值的所以出错!!!!
>>> L[4][2]='Echo'
>>> L
[-17.5, 'name', 55, 'a', ['ram', '2', 'Echo'], 432]
>>> 

   注意:不能为一个位置不存在的元素进行赋值。如果列表长度为2那么不能为索引为100的元素进行赋值,如果要那样做,就必须创建一个长度为101或更长的列表;

  代码示例:

>>> L=[12,'q',45,'ping']
>>> len(L)
4
>>> L[4]
Traceback (most recent call last):
  File "<pyshell#91>", line 1, in <module>
    L[4]
IndexError: list index out of range
>>> L[3]
'ping'
>>> L[3][3]
'g'
>>> L[3][4]
Traceback (most recent call last):
  File "<pyshell#94>", line 1, in <module>
    L[3][4]
IndexError: string index out of range
>>> 

   2.2、删除元素

      从列表中删除元使用del语句实现。

>>> names=['Alice','Beth','Cecil','Dee-dee','Earl']
>>> names
['Alice', 'Beth', 'Cecil', 'Dee-dee', 'Earl']
>>> del names[2]
>>> names
['Alice', 'Beth', 'Dee-dee', 'Earl']
>>> del names[-2:-1]
>>> names
['Alice', 'Beth', 'Earl']
>>> del names[-2:]
>>> names
['Alice']
>>> names=['Alice','Beth','Cecil','Dee-dee','Earl']
>>> names
['Alice', 'Beth', 'Cecil', 'Dee-dee', 'Earl']
>>> del names[::-2]
>>> names
['Beth', 'Dee-dee']

  

   2.3、切片赋值

    切片是一个非常强大的特性;

    

>>> #分片可以一次多个赋值
>>> name=list('Python')
Traceback (most recent call last):
  File "<pyshell#117>", line 1, in <module>
    name=list('Python')
TypeError: 'list' object is not callable
>>> list
[-99, 'name', 55, 'a', ['ram', '2', 'echo'], 234]
>>> name=list('PYthon')
Traceback (most recent call last):
  File "<pyshell#119>", line 1, in <module>
    name=list('PYthon')
TypeError: 'list' object is not callable
>>> del list
>>> list
<class 'list'>
>>> name=list('Perl')
>>> name
['P', 'e', 'r', 'l']
>>> name=list('Python')
>>> name
['P', 'y', 't', 'h', 'o', 'n']
>>> name[5:]=list('ic')
>>> name
['P', 'y', 't', 'h', 'o', 'i', 'c']
>>> name=list('Perl') #使用切片赋值时,可以使用与原序列不等长的序列将切片替换
>>> name
['P', 'e', 'r', 'l']
>>> name[1:]=list('ython')
>>> name
['P', 'y', 't', 'h', 'o', 'n']
>>> #切片赋值语句可以在不需要替换任何原有元素的情况下插入新的元素。
>>> numbers=[1,9]
>>> numbers
[1, 9]
>>> numbers[1:1]=[2,3,4,5,6,7,8]
>>> numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> numbers[4:4]=[0]
>>> numbers
[1, 2, 3, 4, 0, 5, 6, 7, 8, 9]
>>> #通过切片赋值来删除元素可是可行的
>>> numbers[4]=[]
>>> numbers
[1, 2, 3, 4, [], 5, 6, 7, 8, 9]
>>> numbers[4:4]=[]
>>> numbers
[1, 2, 3, 4, [], 5, 6, 7, 8, 9]
>>> numbers[4:4]=[0]
>>> numbers
[1, 2, 3, 4, 0, [], 5, 6, 7, 8, 9]
>>> del numbers[5]
>>> numbers
[1, 2, 3, 4, 0, 5, 6, 7, 8, 9]
>>> numbers[4:4]=[]
>>> numbers
[1, 2, 3, 4, 0, 5, 6, 7, 8, 9]
>>> numbers[4:5]=[]
>>> numbers
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> numbers[1:9]=[]
>>> numbers
[1]
>>> 
原文地址:https://www.cnblogs.com/me80/p/6909275.html