标签:
1、列表
1.1 列表的基本操作:
索引、切片、追加、删除、长度、切片、循环、包含
>>> name_list = [‘alex‘,‘65brother‘,‘tenglan‘] # 初始化一个列表 >>> type(name_list) # 确定类型 <type ‘list‘> >>> help(name_list) # 获取详细帮助信息 >>> dir(name_list) # 获取简单信息 [ .....省略,‘append‘, ‘count‘, ‘extend‘, ‘index‘, ‘insert‘, ‘pop‘, ‘remove‘, ‘reverse‘, ‘sort‘] >>> name_list.append("xiaolan") # 追加一个值 >>> name_list # 查看追加结果 [‘alex‘, ‘65brother‘, ‘tenglan‘, ‘xiaolan‘] >>> name_list.insert(0,"xiaolan") # 通过索引值追加内容 >>> name_list # 查看追加结果 [‘xiaolan‘, ‘alex‘, ‘65brother‘, ‘tenglan‘, ‘xiaolan‘] >>> name_list.count("xiaolan") # 计算“xiaolei”值得总数 >>> name_list.index("65brother") # 查看某个值得索引值 >>> name_list[2] # 通过索引取值 ‘65brother‘ >>> name_list.remove("alex") # 移除扫描到的第一个alex值 >>> name_list [‘xiaolan‘, ‘65brother‘, ‘tenglan‘, ‘xiaolan‘] # 查看移除结果 >>> name_list.pop() # 移除最后一个值,并返回该值 ‘xiaolan‘ >>> name_list # 查看移除效果 [‘xiaolan‘, ‘65brother‘, ‘tenglan‘]
1.2 extend(扩展)
>>> a [1, 2, 3, 4, 6, ‘a‘, ‘b‘] >>> b = [‘c‘,‘d‘,4,24,6] >>> a + b # 两个列表合并 [1, 2, 3, 4, 6, ‘a‘, ‘b‘, ‘c‘, ‘d‘, 4, 24, 6] >>> a.extend(b) # 把b的内容扩展到a列表 >>> a [1, 2, 3, 4, 6, ‘a‘, ‘b‘, ‘c‘, ‘d‘, 4, 24, 6] >>> name = "Alex Li" >>> a.extend(name) # 把字符串的每个字母拆分扩展到a列表 >>> a [1, 2, 3, 4, 6, ‘a‘, ‘b‘, ‘c‘, ‘d‘, 4, 24, 6, ‘A‘, ‘l‘, ‘e‘, ‘x‘, ‘ ‘, ‘L‘, ‘i‘]
1.3 包含
>>> a [1, 2, 3, 4, 6, ‘a‘, ‘b‘, ‘c‘, ‘d‘, 4, 24, 6, ‘A‘, ‘l‘, ‘e‘, ‘x‘, ‘ ‘, ‘L‘, ‘i‘] >>> "A" in a # 字母A是否包含在列表a中 True >>> 24 in a # 数字24 是否包含在列表a中 True
1.4 列表应用场景
案例01:一千万条数据,有100条重复数据,要求删除这100条数据
>>> name_list [‘alex‘, ‘65brother‘, ‘tenglan‘, ‘lanyulei‘, ‘lanyulei‘, ‘lanyulei‘, ‘lanyulei‘] >>> for i in range(name_list.count(‘lanyulei‘)): ... name_list.remove(‘lanyulei‘) ... >>> name_list [‘alex‘, ‘65brother‘, ‘tenglan‘]
1.5 切片
>>> a = [1,2,3,‘a‘,‘b‘] >>> a[0:2] # 从索引为0的值开始,到索引为2的值结束,不包含索引为2的值 [1, 2] >>> a[1:4] # 从索引为1的值开始,到索引为2的值结束,不包含索引为2的值 [2, 3, ‘a‘] >>> a[0:4] [1, 2, 3, ‘a‘] >>> a[0:4:2] # 从索引为0的值开始,到索引为4的值结束,不包含索引为4的值,然后再每隔2个留一个值 [1, 3] >>> a[0:4:3] # 从索引为0的值开始,到索引为4的值结束,不包含索引为4的值,然后再每隔3个留一个值 [1, ‘a‘] >>> a = [1,6,2,4,3,‘a‘,‘b‘] >>> a[-1] # 取倒数第一个值 ‘b‘ >>> a[-2:-1] # 取倒数2个值,然后再取倒数第一个值 [‘a‘] >>> a[-2:] # 取倒数2个值 [‘a‘, ‘b‘] >>> a[:3] # 取前三个值 [1, 6, 2] ‘‘‘ 在Python 3.x中,列表的sort中既有数字又有字符串则sort功能无法使用,如果需要排序则要先实现切片,再进行分片 ‘‘‘ C:\Users\Administrator>python3 Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:16:59) [MSC v.1900 32 bit (In tel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> a = [1,6,2,4,3,‘a‘,‘b‘] >>> b = a.sort() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: str() < int() C:\Users\Administrator>python3 Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:16:59) [MSC v.1900 32 bit (In tel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> a = [1,6,2,4,3,‘a‘,‘b‘] >>> b = a.sort() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: str() < int() # 先切片再排序 >>> a = [1,6,2,4,3,‘a‘,‘b‘] >>> b = a[0:5] >>> b.sort() >>> b [1, 2, 3, 4, 6]
1.6
sort Python3.x
>>> a = [1,5,2,3,‘a‘,‘b‘] >>> a.sort() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: str() < int()
2、元组
元组的作用,元组能实现的功能列表都能实现,工作中使用元组多数是提醒他人这个是一个元组,无法更改内容
>>> t = (1,2,3,4,‘a‘,‘list‘) >>> type(t) <class ‘tuple‘> >>> dir(t) [......省略, ‘count‘, ‘index‘]
元组互相转换列表(实现了元组的修改)
元组-->列表
>>> t (1, 2, 3, 4, ‘a‘, ‘list‘) >>> list(t) [1, 2, 3, 4, ‘a‘, ‘list‘]
列表-->元组
>>> a
[1, 2, 3, 4, 6, ‘a‘, ‘b‘, ‘c‘, ‘d‘, 4, 24, 6, ‘A‘, ‘l‘, ‘e‘, ‘x‘, ‘ ‘, ‘L‘, ‘i‘]
>>> tuple(a)
(1, 2, 3, 4, 6, ‘a‘, ‘b‘, ‘c‘, ‘d‘, 4, 24, 6, ‘A‘, ‘l‘, ‘e‘, ‘x‘, ‘ ‘, ‘L‘, ‘i‘)
标签:
原文地址:http://www.cnblogs.com/madsnotes/p/5485074.html