基准对象object中的基础类型----列表 (四)

时间:2022-01-15 03:30:30

object有如下子类:

CLASSES
object
basestring
str
unicode
buffer
bytearray
classmethod
complex
dict
enumerate
file
float
frozenset
int
bool
list
long
memoryview
property
reversed
set
slice
staticmethod
super
tuple
type
xrange

子类

序列是Python中最基本的数据结构。序列中的每个元素都分配一个数字 -- 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。

1、定义

#创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可。如下所示:
list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5 ]
list3 = ["a", "b", "c", "d"]

2、增

序列内建函数append

L.append(object) -- append object to end

>>>list1=["name","age","address","work"]
>>>list1.append("jack")
>>>list1
Out[37]: ['name', 'age', 'address', 'work', 'jack']
>>>list1.append(["lucy","jack"])
>>>list1
Out[38]: ['name', 'age', 'address', 'work', 'jack', ['lucy', 'jack']]

L.extend(iterable) -- extend list by appending elements from the iterable

>>>list1=["name","age","address","work"]
>>>list1.extend("jack")
>>>list1
Out[48]: ['name', 'age', 'address', 'work', 'j', 'a', 'c', 'k']
>>>list1.extend(["jack","lucy"])
>>>list1
>>>Out[50]: ['name', 'age', 'address', 'work', 'j', 'a', 'c', 'k', 'jack', 'lucy']
>>>list1.extend({1:"one",2:"two"})
>>>list1
Out[52]: ['name', 'age', 'address', 'work', 'j', 'a', 'c', 'k', 'jack', 'lucy', 1, 2]

L.insert(index, object) -- insert object before index

>>>list1=["name","age","address","work","name"]
>>>list1.insert(0, "work")
>>>list1
Out[74]: ['work', 'name', 'age', 'address', 'work', 'name']

3、删

L.pop([index]) -> item -- remove and return item at index (default last).Raises IndexError if list is empty or index is out of range.

L.remove(value) -- remove first occurrence of value.Raises ValueError if the value is not present.

del L ---delete the whole list,so the list doesn't exist.

>>>list1=["name","age","address","work","name"]
>>>list1.pop(0)
Out[65]: 'name'
>>>list1
Out[66]: ['age', 'address', 'work', 'name']
>>>list1.remove("work")
>>>list1
Out[68]: ['age', 'address', 'name']

4、改

>>>list1=["name","age","address","work","name"]
>>>list1[4]="work"
>>>list1
Out[71]: ['name', 'age', 'address', 'work', 'work']

5、查

L.count(value) -> integer -- return number of occurrences of value

>>>list1=["name","age","address","work","name"]
>>>list1.count("name")
Out[59]: 2

L.index(value, [start, [stop]]) -> integer -- return first index of value.Raises ValueError if the value is not present.

>>>list1=["name","age","address","work","name"]
>>>list1.index("name")
Out[60]: 0
>>>list1.index("work")
Out[61]: 3
>>>list1.index("name",1,len(list1))
Out[63]: 4

列表可以使用下标索引来访问列表中的值

>>>a=[1,2,3,4]
>>>a[0]
Out[105]: 1
>>>a[-1]
Out[106]: 4
>>>a[-3:-1]
Out[107]: [2, 3]
>>>a[0:2]
Out[109]: [1, 2]

6、排序

L.reverse() -- reverse *IN PLACE*

L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;cmp(x, y) -> -1, 0, 1

>>>list1=['work', 'name', 'age', 'address', 'work', 'name']
>>>list1.reverse()
>>>list1
Out[77]: ['name', 'work', 'address', 'age', 'name', 'work']
>>>list1.sort()
>>>list1
Out[79]: ['address', 'age', 'name', 'name', 'work', 'work']