创建列表
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 a = ['alex', 'seven', 'eric']4 #或5 a = list(['alex', 'seven', 'eric'])
list转换列表 """(转换成列表,需要转换的可迭代数据变量) 注意:能转换成列表的必须是可迭代的,也就是可以被for循环的"""
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 #list将16进制转换成字符串4 a = "林贵秀"5 b = list(a)#将一个字符串转换成一个列表,只要是可以被for循环的都可以用list转换成列表6 print(b)7 #输出 ['林', '贵', '秀']
字符串,元组,列表 > 都可以转换成列表,转换成列表都是可以被for循环的,for循环每次循环到的数据就是列表的一个元素
基本操作:
索引 切片 追加 删除 长度 循环 包含
打印列表里的元素
打印出列表里的元素是以列表变量加元素下标的方式来打印
索引
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 a = ['alex', 'seven', 'eric']4 print(a[0])5 print(a[1])6 #输出 alex seven 这样就打印出了,第零个和第一个下标的元素
切片
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 #切片4 a = ['alex', 'seven', 'eric']5 print(a[0:2])6 #输出 alex seven 这样就切片出了,第零一个和第二个下标的元素
统计列表里有多少个元素
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 a = ['alex', 'seven', 'eric']4 print(len(a))5 #输出 3 统计列表里有多少个元素
循环列表
while循环
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 #while循环4 a = ['alex', 'seven', 'eric']5 b = 06 while b < len(a): #len(统计列表里的元素)7 print(a[b])8 b += 19 #循环出列表里的所有元素
for循环
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 #for循环4 a = ['alex', 'seven', 'eric']5 for b in a: #b为自定义循环变量6 print(b)7 #循环出列表里的所有元素
append(self, p_object)
"""(追加列表)"""要追加的元素
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 a = ['alex', 'seven', 'eric']4 a.append("linguixiu")5 print(a)6 #打印出 ['alex', 'seven', 'eric', 'linguixiu'] 也就是追加了一个元素
count(self, value)
"""(统计元素在列表里出现的次数)"""要统计的元素
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 a = ['alex', 'seven', 'eric', 'seven',]4 b = a.count("seven")5 print(b)6 #打印出 2 统计到seven在列表中出现两次
extend(self, iterable)
"""(扩展列表)"""要扩展的可迭代变量:可迭代是只要是能通过for循环出来的都为可迭代
也就是可以将一个列表扩展到另一个列表
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 a = ['alex', 'seven', 'eric', 'seven',]4 b = ["1", "2", "3"]5 a.extend(b)6 print(a)7 #打印出 ['alex', 'seven', 'eric', 'seven', '1', '2', '3'] 将一个列表扩展到另一个列表
index(self, value, start=None, stop=None)
"""(获取一个元素在列表里的索引下标)"""要获取的元素
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 a = ['alex', 'eric', 'seven']4 print(a.index("seven"))5 #打印出 2 获取到seven的索引下标为2,默认从0开始所以是2
insert(self, index, p_object)
"""(插入元素)"""要插入的位置,要插入的元素
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 a = ['alex', 'eric', 'seven']4 a.insert(0,"guixiu")5 print(a)6 #打印出 ['guixiu', 'alex', 'eric', 'seven'] 在0的位置插入guixiu
pop(self, index=None)
"""(移除元素)"""要移除的位置:默认移除后面一个元素
移除的元素还可以重新赋值给一个变量
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 a = ['alex', 'eric', 'seven']4 b = a.pop(0)#移除a变量的0位置元素,将移除的元素赋值给b5 print(a)6 #打印a ['eric', 'seven'] 移除了0位置的元素7 8 print(b)9 #打印b alex 被移除的元素
remove(self, value)
"""(移除某个元素)"""要移除的元素
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 a = ['alex', 'eric', 'seven']4 a.remove("eric")5 print(a)6 #打印出 ['alex', 'seven'] 移除了eric
reverse(self)
"""(反转元素顺序)"""
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 a = ['alex', 'eric', 'seven']4 a.reverse()5 print(a)6 #打印出 ['seven', 'eric', 'alex'] 反转元素顺序
删除元素
索引方式删除(删除单个元素)
格式:del 列表变量[要删除的下标]
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 a = ['alex', 'eric', 'seven']4 del a[0]5 print(a)6 #打印出 ['eric', 'seven'] 删除了0位置的元素
切片方式删除(删除多个元素)
格式:del 列表变量[开始位置:结束位置]
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 a = ['alex', 'eric', 'seven']4 del a[0:2]5 print(a)6 #打印出 ['seven'] 删除了0和2位置的元素
clear(self)
"""(移除列表所有元素)"""
1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 #list将16进制转换成字符串4 a = ['林', '贵', '秀']5 a.clear()6 print(a)
enumerate(iterable,start=0 )
"""(自定义列表的下标开始位置)列表变量,要定义的开下标数:默认是0开始的"""
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 a = ["电脑", "鼠标", "键盘", "显示器"] 4 for k,v in enumerate(a,1):#因为是有键和值,所以循环要定义两个变量k和v 5 print(k,v) 6 #打印出 7 # 1 电脑 8 # 2 鼠标 9 # 3 键盘 10 # 4 显示器
enumerate()结合应用,输入商品序号,打印出对应的商品
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #输入商品序号,打印出对应的商品 4 a = ["电脑", "鼠标", "键盘", "显示器"] 5 for k,v in enumerate(a,1):#默认下标号从0开始的enumerate(a,1)设置了从1开始 6 print(k,v) 7 #上面打印出商品的序号和名称 8 b = input("请输入商品序号")#等待用户输入商品序号 9 c = int(b)#将用户输入的序号转换成数字类型 10 d = a[c-1]#将用户输入序号转换成列表索引的下标,默认从0开始的所以要减一 11 print(d)#通过用户输入的索引下标打印出商品 12 13 # 1 电脑 14 # 2 鼠标 15 # 3 键盘 16 # 4 显示器 17 # 请输入商品序号4 18 # 显示器
列表的嵌套,列表里有字典,元组,
用索引的方法输出需要的省份,省会,以及一个市1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #列表的嵌套,列表里有字典,元组, 4 #用索引的方法输出需要的省份,省会,以及一个市 5 a = ['四川', { '省会': '成都'}, ('自贡', '内江', '乐山'), '云南', { '省会': '昆明'}, ('曲靖', '玉溪', '昭通')] 6 print(a[0])#=四川, 索引列表第0个元素 7 print(a[1])#{'省会': '成都'}, 索引列表第1个元素 8 print(a[1]["省会"])#=成都, 索引列表第1个元素,里的字典的"省会"这个键 9 print(a[2])#=('自贡', '内江', '乐山') 索引列表里的第二个元素10 print(a[2][0])#=自贡 索引列表里第2个元素,里的元组的第0个元素11 #最终输出12 # 四川13 # {'省会': '成都'}14 # 成都15 # ('自贡', '内江', '乐山')16 # 自贡
class list(object): """ list() -> new empty list list(iterable) -> new list initialized from iterable's items """ def append(self, p_object): # real signature unknown; restored from __doc__ """ L.append(object) -- append object to end """ pass def count(self, value): # real signature unknown; restored from __doc__ """ L.count(value) -> integer -- return number of occurrences of value """ return 0 def extend(self, iterable): # real signature unknown; restored from __doc__ """ L.extend(iterable) -- extend list by appending elements from the iterable """ pass def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ """ L.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. """ return 0 def insert(self, index, p_object): # real signature unknown; restored from __doc__ """ L.insert(index, object) -- insert object before index """ pass def pop(self, index=None): # real signature unknown; restored from __doc__ """ L.pop([index]) -> item -- remove and return item at index (default last). Raises IndexError if list is empty or index is out of range. """ pass def remove(self, value): # real signature unknown; restored from __doc__ """ L.remove(value) -- remove first occurrence of value. Raises ValueError if the value is not present. """ pass def reverse(self): # real signature unknown; restored from __doc__ """ L.reverse() -- reverse *IN PLACE* """ pass def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__ """ L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*; cmp(x, y) -> -1, 0, 1 """ pass def __add__(self, y): # real signature unknown; restored from __doc__ """ x.__add__(y) <==> x+y """ pass def __contains__(self, y): # real signature unknown; restored from __doc__ """ x.__contains__(y) <==> y in x """ pass def __delitem__(self, y): # real signature unknown; restored from __doc__ """ x.__delitem__(y) <==> del x[y] """ pass def __delslice__(self, i, j): # real signature unknown; restored from __doc__ """ x.__delslice__(i, j) <==> del x[i:j] Use of negative indices is not supported. """ pass def __eq__(self, y): # real signature unknown; restored from __doc__ """ x.__eq__(y) <==> x==y """ pass def __getattribute__(self, name): # real signature unknown; restored from __doc__ """ x.__getattribute__('name') <==> x.name """ pass def __getitem__(self, y): # real signature unknown; restored from __doc__ """ x.__getitem__(y) <==> x[y] """ pass def __getslice__(self, i, j): # real signature unknown; restored from __doc__ """ x.__getslice__(i, j) <==> x[i:j] Use of negative indices is not supported. """ pass def __ge__(self, y): # real signature unknown; restored from __doc__ """ x.__ge__(y) <==> x>=y """ pass def __gt__(self, y): # real signature unknown; restored from __doc__ """ x.__gt__(y) <==> x>y """ pass def __iadd__(self, y): # real signature unknown; restored from __doc__ """ x.__iadd__(y) <==> x+=y """ pass def __imul__(self, y): # real signature unknown; restored from __doc__ """ x.__imul__(y) <==> x*=y """ pass def __init__(self, seq=()): # known special case of list.__init__ """ list() -> new empty list list(iterable) -> new list initialized from iterable's items # (copied from class doc) """ pass def __iter__(self): # real signature unknown; restored from __doc__ """ x.__iter__() <==> iter(x) """ pass def __len__(self): # real signature unknown; restored from __doc__ """ x.__len__() <==> len(x) """ pass def __le__(self, y): # real signature unknown; restored from __doc__ """ x.__le__(y) <==> x<=y """ pass def __lt__(self, y): # real signature unknown; restored from __doc__ """ x.__lt__(y) <==> xx*n """ pass @staticmethod # known case of __new__ def __new__(S, *more): # real signature unknown; restored from __doc__ """ T.__new__(S, ...) -> a new object with type S, a subtype of T """ pass def __ne__(self, y): # real signature unknown; restored from __doc__ """ x.__ne__(y) <==> x!=y """ pass def __repr__(self): # real signature unknown; restored from __doc__ """ x.__repr__() <==> repr(x) """ pass def __reversed__(self): # real signature unknown; restored from __doc__ """ L.__reversed__() -- return a reverse iterator over the list """ pass def __rmul__(self, n): # real signature unknown; restored from __doc__ """ x.__rmul__(n) <==> n*x """ pass def __setitem__(self, i, y): # real signature unknown; restored from __doc__ """ x.__setitem__(i, y) <==> x[i]=y """ pass def __setslice__(self, i, j, y): # real signature unknown; restored from __doc__ """ x.__setslice__(i, j, y) <==> x[i:j]=y Use of negative indices is not supported. """ pass def __sizeof__(self): # real signature unknown; restored from __doc__ """ L.__sizeof__() -- size of L in memory, in bytes """ pass __hash__ = None
列表的功能
转换列表
索引切片for循环长度删除元素反转排序追加插入移除元素索引位置统计元素个数扩展清除