Python学习笔记(一)——列表
2018-01-31 17:01
399 查看
列表
一个数组中可以有字符串,也可以有数字
>>> 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][1][1] Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> spam[1][1][1] TypeError: 'int' object is not subscriptable >>> spam[1][1] 20 >>> spam[-1] [10, 20, 30]
利用切片获取子列表
>>> spam ['a', 'b', 'c', 'd'] >>> spam[1:3] ['b', 'c'] >>> spam[1:-1] ['b', 'c'] >>>
列表函数
len
>>> len(spam) 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从列表中删除
>>> spam ['a', 'b', 'c', 'd'] >>> del spam[1] >>> spam ['a', 'c', 'd']
for循环和列表
>>> for i in [1,2,3]: print(spam) ['a', 'c', 'd'] ['a', 'c', 'd'] ['a', 'c', 'd']
另一种情况
>>> for i in [2,3,4,6]: print(i) 2 3 4 6
in 和 not in 操作
>>> spam ['a', 'c', 'd'] >>> a = 'a' >>> a in spam True >>> temp = 'f' >>> temp not in spam True
多重赋值技巧
>>> a,b,c = spam >>> spam ['a', 'c', 'd'] >>> a 'a' >>> b 'c' >>> c 'd'
方法
index()方法在列表中查找值
spam = ['a','b','c','d'] while True: a = input() if(a == 'exit'): break try: print(spam.index(a)) except ValueError: print('Error')
>>> spam ['a', 'c', 'd'] >>> a 'a' >>> spam.index(a) 0 #返回的是位置消息
append()和insert()的使用
>>> spam ['a', 'c', 'd'] >>> spam.append(a) >>> spam ['a', 'c', 'd', 'a'] #在列表最后添加项目 >>> spam.insert(1,'temp') >>> spam ['a', 'temp', 'c', 'd', 'a'] #在指定位置添加制定项
remove()方法从列表中删除值
>>> spam ['a', 'temp', 'c', 'd', 'a'] >>> spam.remove('temp') >>> spam ['a', 'c', 'd', '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排序>>> s ['c', 'd', 'a', 1, 5, 4, 3, 3.1] >>> d = s[0:3] >>> d ['c', 'd', 'a'] >>> d.sort() >>> d ['a', 'c', 'd'] >>> s[0:3] = d >>> s ['a', 'c', 'd', 1, 5, 4, 3, 3.1]
如果列表中的元素不一致的话,无法排序
#可以int,float混合排序 >>> f [1, 5, 4, 3, 3.14] >>> f.sort() >>> f [1, 3, 3.14, 4, 5]
逆向排序
#reverse=True >>> f [1, 3, 3.14, 4, 5] >>> f.sort(reverse=True) >>> f [5, 4, 3.14, 3, 1]
按照字典排序
注:
key = str.lower是把所有元素当作小写·
>>> spam = ['a','Z','z','A'] >>> spam.sort(key=str.lower) >>> spam ['a', 'A', 'Z', 'z']
元组数据类型
元组的内容是不可改变的,其次Python对元组进行了优化,使用速度更快>>> sp = ('hello',99,3.14) >>> type(('hello',)) <class 'tuple'> >>> type(('hello')) <class 'str'> #---------------------------- >>> sp = ('hello',99,3.14) >>> sp[1] 99 >>> sp[1] = 44 Traceback (most recent call last): File "<pyshell#164>", line 1, in <module> sp[1] = 44 TypeError: 'tuple' object does not support item assignment
使用list()和tuple()函数来转换类型
>>> list(('who','are','you')) ['who', 'are', 'you'] >>> list(('hello')) ['h', 'e', 'l', 'l', 'o'] >>> tuple(['hello','word']) ('hello', 'word') >>> tuple(['hello']) ('hello',)
引用
在赋值时并未复制,而是指向同一个列表>>> spam [1, 5, 4, 2, 'hello', 'Z', 'z'] >>> spam [1, 5, 4, 2, 'hello', 'Z', 'z'] >>> chees = spam >>> chees[1] = 'hello' >>> spam [1, 'hello', 4, 2, 'hello', 'Z', 'z'] >>> chees [1, 'hello', 4, 2, 'hello', 'Z', 'z']
传递应用(类似于引用调用)
def eggs(temp): temp.append('hello') spam = [1,2,3] eggs(spam) print(spam) =============== RESTART: D:/python/引用调用.py =============== [1, 2, 3, 'hello']
copy()函数和deepcopy()函数
copy.copy()可以用来复制列表和字典这样的可变值,而不只是复制引用
>>> 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()
>>> spam [[1, 2, 3, 'OK'], 1, 2] >>> temp1 = copy.deepcopy(spam) >>> temp1 [[1, 2, 3, 'OK'], 1, 2]
相关文章推荐
- python学习笔记.2之列表
- Python学习笔记之列表
- python学习笔记 迭代和列表生成式
- Python学习笔记(4)- 列表的组织
- Python学习笔记——列表①
- Python学习笔记(六):列表和字典
- python学习笔记3-核心类型-列表
- python 学习笔记 二 序列, 列表, 元组, 字符串
- Python学习笔记二 列表和元组
- python cookbook第三版学习笔记三:列表以及字符串
- 零基础入门学习Python学习笔记第五讲【列表】
- python学习笔记.4创建数值列表
- python学习笔记14 list列表数组
- python学习笔记.4使用列表的一部分,创建切片
- python学习笔记之列表操作
- python学习笔记之列表(list)与元组(tuple)详解
- 【Python】学习笔记——-4.3、列表生成式
- python学习笔记---列表和元组
- Python学习笔记——print&列表
- Python基础教程 第2章: 列表和元组 学习笔记