python列表
来源:互联网 发布:mac口红推荐色号 编辑:程序博客网 时间:2024/06/05 07:21
一、列表的基本知识
列表的定义:
空列表:
>>> [] #空的列表[]>>> type([])<type 'list'>>>> a = []>>> bool(a)False>>>
**注意:**python的列表中的元素可以是任何python对象,可以是字典、集合、列表等等。
如下例子,列表中元素可以是任何对象:
>>> a = ["laoshi",25,89.9,["laoshi",25,89.9]]>>> a['laoshi', 25, 89.9, ['laoshi', 25, 89.9]]
列表的索引和切片:
简单取出:
>>> a = ["laoshi",25,89.9,["laoshi",25,89.9]]>>> a[0] #列表索引是从0开始的'laoshi'>>> a[1]25>>> a[2]89.9>>> a[0:2]['laoshi', 25]>>> a[:2] #[]中左边的值不写,表示从头开始['laoshi', 25]>>> a[0:] #[]中右边的值不写,意思就是到结尾为止['laoshi', 25, 89.9, ['laoshi', 25, 89.9]]>>>
从列表右边开始的话,-1 是第一个索引:
>>> a = ["laoshi",25,89.9,["laoshi",25,89.9]]>>> a[-1]['laoshi', 25, 89.9]
取出列表中的列表中的元素,例如:
>>> a = ["laoshi",25,89.9,["laoshi",25,89.9]]>>> a[3]['laoshi', 25, 89.9]>>> a[3][0] #相当于二维列表'laoshi'>>>
获取的列表的索引,即下标,例如:
>>> a = ["laoshi",25,89.9,["laoshi",25,89.9]]>>> a.index(25)1>>> a.index("laoshi")0
列表的反转
第一种写法:
>>> lst = [1,2,3,4,5,6]>>> lst[::-1] # -1是指步长为1,并代表反方向[6, 5, 4, 3, 2, 1]>>> lst[0:4:2] # 0为起始值,4为末尾值,2为步长,并代表正方向[1, 3]>>>
第二种写法:
>>> lst = [1,2,3,4,5,6]>>> list(reversed(lst))[6, 5, 4, 3, 2, 1]>>>
列表的基本操作:
列表的长度 len 的操作,例如:
>>> lst = [1,2,3,4,5,6]>>> len(lst)6
”+“的加法操作,例如:
>>> a = ["laoshi",25,89.9,["laoshi",25,89.9]]>>> lst = [1,2,3,4,5,6]>>> lst + a[1, 2, 3, 4, 5, 6, 'laoshi', 25, 89.9, ['laoshi', 25, 89.9]]>>>
“ * ”乘法的操作,作用是列表元素重复,例如:
>>> a = ["laoshi",25,89.9,["laoshi",25,89.9]]>>> a*2['laoshi', 25, 89.9, ['laoshi', 25, 89.9], 'laoshi', 25, 89.9, ['laoshi', 25, 89.9]]>>>
in、max()、min()、cmp() 的操作,例如:
>>> a = ["laoshi",25,89.9,["laoshi",25,89.9]]>>> lst = [1,2,3,4,5,6]>>> 25 in a # ni的操作表示该元素是否在列表中True>>> max(lst)6>>> min(lst)1>>> max(a)'laoshi'>>> min(a)25>>> cmp(a,lst)1>>>
二、列表的常用函数
追加和扩展:append、Extend
>>> dir(list)['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__','__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__','__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__','__reduce__','__reduce_ex__', '__repr__','__reversed__', '__rmul__','__setattr__', '__setitem__', '__setslice__','__sizeof__','__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop','remove', 'reverse', 'sort']>>>
append()的用法:
>>> help(list.append)Help on method_descriptor:append(...) L.append(object) -- append object to end>>>
举例如下:
>>> a = [1,2]>>> a.append(100)>>> a[1, 2, 100]>>> a.append(["google","facebook"])>>> a[1, 2, 100, ['google', 'facebook']]>>>
extend()的用法:
>>> help(list.extend)Help on method_descriptor:extend(...) L.extend(iterable) -- extend list by appending elements from the iterable>>>
说明extend(iterable)中的iterable必须是可迭代的,这点重要。
举例如下:
>>> a = [1,2,3]>>> b = [4,5,6]>>> a.extend(b)>>> a[1, 2, 3, 4, 5, 6]>>> a.extend("python") #注意,此处字符串"python"是不可迭代的,但会将字符串中每个字符一个一个地取出,并输出。>>> a[1, 2, 3, 4, 5, 6, 'p', 'y', 't', 'h', 'o', 'n']>>> a.extend(5) # 5 不是可迭代的,所以会报如下错误Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'int' object is not iterable>>>
可迭代的判断方法:
>>> b = [1,2]>>> hasattr(b,'__iter__')True>>> hasattr("python",'__iter__')False>>> hasattr(5,'__iter__')False>>>
append()和extend()的重要区别:
>>> a= [1,2] >>> a.append([4,5]) #append的用法>>> a[1, 2, [4, 5]]>>>>>>>>> a = [1,2]>>> a.extend([4,5]) #extend的用法>>> a[1, 2, 4, 5]>>>
其他函数,如Count()、Index()、Insert()、Pop()、Remove()、Reverse()、Sort()
Count()的用法:
>>> help(list.count)Help on method_descriptor:count(...) L.count(value) -> integer -- return number of occurrences of value>>>
count(value):表示value的值在列表中出现的次数,例如:
>>> a = [1,1,1,2,2,1]>>> a.count(1)4>>> a.count(2)2
Index()的用法:
>>> help(list.index)Help on method_descriptor:index(...) L.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present.>>>
index(value)返回的是value所在的第一个索引值,例如:
>>> a[1, 1, 1, 2, 2, 1]>>> a.index(1)0>>> a.index(2)3>>>
Insert()的用法:
>>> help(list.insert)Help on method_descriptor:insert(...) L.insert(index, object) -- insert object before index>>>
举例如下:
>>> a = ["python","web"]>>> a.insert(1,"Xiaoxu")>>> a['python', 'Xiaoxu', 'web']>>> a.insert(0,20161111)>>> a[20161111, 'python', 'Xiaoxu', 'web']>>>
Pop()的用法:
>>> help(list.pop)Help on method_descriptor:pop(...) L.pop([index]) -> item -- remove and return item at index (default last). Raises IndexError if list is empty or index is out of range.>>>
举例如下:
>>> a = [20161111, 'python', 'Xiaoxu', 'web']>>> a.pop(2)'Xiaoxu'>>>
Remove()的用法:
>>> help(list.remove)Help on method_descriptor:remove(...) L.remove(value) -- remove first occurrence of value. Raises ValueError if the value is not present.>>>
若有多个相同的值,remove()只能删除第一个值
举例如下:
>>> a = ["taishan","taishan","huangshan"]>>> a.remove("taishan")>>> a['taishan', 'huangshan']>>>
Reverse()的用法:
>>> help(list.reverse)Help on method_descriptor:reverse(...) L.reverse() -- reverse *IN PLACE*>>>
举例如下:
>>> a = [1,2,3,4]>>> a.reverse()>>> a[4, 3, 2, 1]>>>
Sort()的用法:
>>> help(list.sort)Help on method_descriptor:sort(...) L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; cmp(x, y) -> -1, 0, 1>>>
举例如下:
>>> a = [5,3,9,2]>>> a.sort() #默认的是升序排列>>> a[2, 3, 5, 9]>>>>>> b = [5,3,9,2]>>> b.sort(reverse=True) #此处设置降序排列>>> b[9, 5, 3, 2]>>>
0 0
- Python 列表
- python 列表
- python--列表
- python 列表
- Python 列表
- Python 列表
- python 列表
- Python列表
- python列表
- Python列表
- Python列表
- python列表
- Python列表
- python 列表
- python列表
- python列表
- Python--列表
- Python-列表
- linux-rz sz yum方式 安装
- 【随记】common-xxx.jar常用工具类
- 解决iOS手势影响其他控件问题
- Codewars简单使用和 其中一道题,用JavaScript实现一个函数,求一个正数的次大数字,拿自己的解法和大神相比,简直对不起JavaScript给我提供的 那么多便利
- php正则匹配动态页面文章标题<h1>
- python列表
- BZOJ 2878 Noi2012 迷失游乐园
- Linux安装配置apache
- 魔方 水题
- Xcode自动断点在[recorder prepareToRecord]
- jar包冲突--Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lcom/an
- 干货:结合Scikit-learn介绍几种常用的特征选择方法
- 虚拟现实-VR-UE4-编译UE4源码
- Centos下安装apahce的configure: error: APR not found. Please read the documentation解决办法