Python列表

列表

一个数组中可以有字符串,也可以有数字

1
2
3
4
5
6
7
8
9
10
11
12
13
>>> spam = [['cat','bat'],[10,20,30]]
>>> spam[1]
[10, 20, 30]
>>> spam[0]
['cat', 'bat']
>>> spam[0][0]
'cat'
>>> spam[1][0]
10
>>> spam[0][0][0]
'c'
>>> spam[-1]
[10, 20, 30]

利用切片获取子列表

1
2
3
4
5
6
7
>>> spam
['a', 'b', 'c', 'd']
>>> spam[1:3]
['b', 'c']
>>> spam[1:-1]
['b', 'c']
>>>

列表函数

len

1
2
>>> len(spam)
4

操作符 ‘*’,’+’运算

1
2
3
4
>>> ['a','c','d']+[1,2,3,4]
['a', 'c', 'd', 1, 2, 3, 4]
>>> (['a','c','d']+[1,2,3,4])*3
['a', 'c', 'd', 1, 2, 3, 4, 'a', 'c', 'd', 1, 2, 3, 4, 'a', 'c', 'd', 1, 2, 3, 4]

del从列表中删除

1
2
3
4
5
>>> spam
['a', 'b', 'c', 'd']
>>> del spam[1]
>>> spam
['a', 'c', 'd']

for循环和列表

1
2
>>> for i in [1,2,3]:
print(spam[i])

in 和 not in 操作

1
2
3
4
5
6
7
8
9
>>> spam = ['a', 'b', 'c', 'd']
>>> spam
['a', 'b', 'c', 'd']
>>> a = 'a'
>>> a in spam
True
>>> temp = 'f'
>>> temp not in spam
True

多重赋值技巧

1
2
3
4
5
6
7
>>> spam = ['a', 'b', 'c', 'd']
>>> a,b,c,d = spam
>>> a
'a'
>>> spam[2:4] = ['1', '2']
>>> spam
['a', 'b', '1', '2']

方法

index()方法在列表中查找值

1
2
3
4
5
>>> spam = ['a','b','c']
>>> spam
['a', 'b', 'c']
>>> spam.index('a')
0

append()和insert()的使用

1
2
3
4
5
6
7
8
>>> spam
['a', 'b', 'c']
>>> spam.append(a)
>>> spam
['a', 'b', 'c', 'a'] #在列表最后添加项目
>>> spam.insert(1, 'temp')
>>> spam
['a', 'temp', 'b', 'c', 'a'] #在指定位置添加制定项

remove()方法从列表中删除值

1
2
3
4
5
6
7
8
9
10
>>> spam
['a', 'temp', 'b', 'c', 'a']
>>> spam.remove('temp')
>>> spam
['a', 'b', 'c', 'a']
>>> spam.remove('g')
Traceback (most recent call last):
File "<pyshell#86>", line 1, in <module>
spam.remove('g')
ValueError: list.remove(x): x not in list

sort()将列表中的值的排序

默认按照ASCII排序

1
2
3
4
>>> s = ['c', 'b', 'a', 1, 5, 3, 3.1]
>>> s.sort()
>>> s
[1, 3, 3.1, 5, 'a', 'b', 'c']

逆向排序

1
2
3
4
>>> s = [1, 4, 3.14, 3, 5]
>>> s.sort(reverse=True)
>>> s
[5, 4, 3.14, 3, 1]

按照字典排序

注:key = str.lower 是把所有元素当作小写·

1
2
3
4
>>> spam = ['a','Z','z','A']
>>> spam.sort(key=str.lower)
>>> spam
['a', 'A', 'Z', 'z']

元组数据类型

元组的内容是不可改变的,其次Python对元组进行了优化,使用速度更快

1
2
3
4
5
6
7
8
9
10
11
12
13
>>> sp = ('hello',99,3.14)
>>> type(sp)
<type 'tuple'>
>>> type(('hello',))
<class 'tuple'>
>>> type(('hello'))
<class 'str'>
>>> sp[1]
99
>>> sp[1] = 88
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

使用list()和tuple()函数来转换类型

1
2
3
4
5
6
7
8
>>> list(('who','are','you'))
['who', 'are', 'you']
>>> list(('hello'))
['h', 'e', 'l', 'l', 'o']
>>> tuple(['hello','word'])
('hello', 'word')
>>> tuple(['hello'])
('hello',)

引用

在赋值时并未复制,而是指向同一个列表

1
2
3
4
5
6
7
8
>>> spam
[1, 5, 4, 2, 'hello', 'Z', 'z']
>>> chees = spam
>>> chees[2] = 'world'
>>> spam
[1, 5, 'world', 2, 'hello', 'Z', 'z']
>>> chees
[1, 5, 'world', 2, 'hello', 'Z', 'z']

传递应用(类似于引用调用)

1
2
3
4
5
6
def eggs(temp):
temp.append('hello')
spam = [1,2,3]
eggs(spam)
print(spam)
#[1, 2, 3, 'hello']

copy()函数和deepcopy()函数

copy.copy() 可以用来复制列表和字典这样的可变值,而不只是复制引用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>>> import copy
>>> spam = [1,2,3]
>>> spam
[1, 2, 3]
>>> temp = copy.copy(spam)
>>> temp
[1, 2, 3]
>>> spam.append('hello')
>>> spam
[1, 2, 3, 'hello']
>>> temp
[1, 2, 3]
>>> temp.append('OK')
>>> spam,temp
([1, 2, 3, 'hello'], [1, 2, 3, 'OK'])

如果说列表中包含了列表,则应当使用copy.deepcopt()

1
2
3
4
5
>>> spam
[[1, 2, 3, 'OK'], 1, 2]
>>> temp = copy.deepcopy(spam)
>>> temp
[[1, 2, 3, 'OK'], 1, 2]

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :