Python列表操作

 列表简介:

 

列表是序列对象,可包含任意的Python数据信息,如字符串、数字、列表、元组等
 列表的数据是可变的,我们可通过对象方法对列表中的数据进行增加、修改、删除等操作
 可以通过list(seq)函数把一个序列类型转换成一个列表

 列表的数据是可变的,我们可通过对象方法对列表中的数据进行增加、修改、删除等操作
 可以通过list(seq)函数把一个序列类型转换成一个列表

 

列表对象支持的方法:

  •  1.append(x),在列表尾部追加单个对象x,使用多个参数会引起异常
  •  2.count(x),返回对象x在列表中出现的次数
  •  3.extend(L),将列表L中的表项添加到列表中,返回None
  •  4.Index(x),返回列表中匹配对象x的第一个列表项的索引,无匹配元素时产生异常
  •  5.insert(i,x),在索引为i的元素前插入对象x,如list.insert(0,x)在第一项前插入对象,返回None
  •  6.pop(x),删除列表中索引为x的表项,并返回该表项的值,若未指定索引,pop返回列表最后一项
  •  7.remove(x),删除列表中匹配对象x的第一个元素,匹配元素时产生异常,返回None
  •  8.reverse(),颠倒列表元素的顺序
  •  9.sort(),对列表排序,返回none,bisect模块可用于排序列表项的添加和删除


几个实例:

1.获取有关 list 的帮助

>>> help(list)
Help on class list in module __builtin__:
class list(object)
  list() -> new list
|  list(sequence) -> new list initialized from sequence's items
| 
|  Methods defined here:
| 
|  __add__(...)
|      x.__add__(y) <==> x+y
| 
|  __contains__(...)
|      x.__contains__(y) <==> y in x
| 
...

2.本例展示如何创建包含从0到9(包括0和9)的简单list,以及如何创建一个空列表和一个包含单个条目的列表

>>> l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> type(l)
<type 'list'>
>>> el = []    # Create an empty list
>>> len(el)
0
>>> sl = [1]    # Create a single item list
>>> len(sl)
1

3.直接创建 list 对象,将序列直接传递给构造函数,还可以将拥有元组或字符串的变量传递给 list 构造函数

>>> l = list()
>>> type(l)
<type 'list'>
>>> len(l)
0
>>> l
[]
>>> l = list((0, 1, 2, 3, 4, 5, 6, 7, 8, 9))    # Create a list from a tuple
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> len(l)
10
>>> l = list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])    # Create a list from a list
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> len(l)
10
>>> l = list(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)     
 Error: Must pass in a sequence
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: list() takes at most 1 argument (10 given)
>>> l = list("0123456789") # Create a list from a string
>>> l
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> type(l)
<type 'list'>
>>> len(l)
10

4.从 list 访问条目

>>> l = list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> l[0]         # Get the first item in the list
0
>>> type(l[0])
<type 'int'>
>>> l[5]         # Get the sixth item in the list
5
>>> l[1:5]       # Get the second through fifth items
[1, 2, 3, 4]
>>> type(l[1:5])
<type 'list'>
>>> l[0::2]      # Get every second item
[0, 2, 4, 6, 8]
>>> l[0], l[1], l[2]   
(0, 1, 2)

注:
 切片是一个非常有用的概念,其一般形式为 l[start:end:step],其中 start 和 end 分别是开始和结束索引,step 是在切片时要跨过的条目数量
 此外,还可以对结束索引使用负值,即从序列的结尾往回计数
 另一个有用的功能是以一种很合适的方式处理错误(如超过序列的长度),如前一个例子所示,还可以选择忽略切片中使用的三个值中的一个或多个值

5.可变的序列,修改 list

本文的开头,提到过list 是一个可变的序列,这就意味着您不但可以方便地访问 list 中的条目,而且可以方便地修改它们,但这会引起一个并发症状:您只能修改序列中的条目,若要向序列中添加条目(而不仅仅是修改条目),可使用 append 方法
>>> l = []
>>> l[0] = 0      # The list is empty
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IndexError: list assignment index out of range
>>> l.append(0)
>>> l
[0]
>>> l[0] = 1
>>> l
[1]

6.异构的可变序列

list可以持有不同类型的数据(或不同类型的对象),即异构的可变序列
>>> l=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l[2] = 2
>>> type(l[2])
<type 'int'>
>>> l[2] = "two"      # Change the type of an element
>>> type(l[2])
<type 'str'>
>>> l
[0, 1, 'two', 3, 4, 5, 6, 7, 8, 9]
>>> l[2] = l[2:5] * 2
>>> l
[0, 1, ['two', 3, 4, 'two', 3, 4], 3, 4, 5, 6, 7, 8, 9]
>>> del(l[2])         # Remove single element
>>> l
[0, 1, 3, 4, 5, 6, 7, 8, 9]
>>> l[1:3] = []       # Remove a slice
>>> l
[0, 4, 5, 6, 7, 8, 9]

注:
 本例展示了如何向 list 中添加元素,以及如何修改 list 中的条目
 还演示了如何从 list 中删除对象;删除条目的第一个方法是使用 del 方法,使用此方法可以删除一个条目或一个条目范围;还可以使用灵活而强大的切片方法从 list 中删除切片


转自:http://www.2cto.com/kf/201206/137662.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值