列表的生成
>>> language = [] #生成一个空列表 >>> language [] >>> language = list() #生成一个空列表 >>> language [] >>> language = [‘python‘,‘php‘,‘java‘,‘python‘,‘asp‘,‘perl‘,‘python‘,‘javascript‘] #生成一个列表 >>> language = [‘python‘,‘php‘,‘java‘,‘python‘,[‘asp‘,‘perl‘,‘python‘],‘javascript‘] #列表嵌套
列表中值的查询
>>> language = [‘python‘,‘php‘,‘java‘,‘python‘,‘asp‘,‘perl‘,‘python‘,‘javascript‘] >>> language[0] #获取列表中索引为0的值 ‘python‘ >>> language[2] ‘java‘ >>> language[-1] #获取列表中索引为-1的值 ‘javascript‘ >>> language.index(‘php‘) #获取值的索引 1 >>> language.index(‘python‘) #获取值的索引,从左往右第一个 0
列表中值的个数
>>> language.count(‘python‘) #获取列表中指定值的个数 3 >>> language.count(‘asp‘) 1
列表的切片
>>> language = [‘python‘,‘php‘,‘java‘,‘python‘,‘asp‘,‘perl‘,‘python‘,‘javascript‘] >>> language[:] #获取整个列表的数据 [‘python‘, ‘php‘, ‘java‘, ‘python‘, ‘asp‘, ‘perl‘, ‘python‘, ‘javascript‘] >>> language[1:4] #获取索引1到3的数据 [‘php‘, ‘java‘, ‘python‘] >>> language[:5] #获取前5个数据 [‘python‘, ‘php‘, ‘java‘, ‘python‘, ‘asp‘] >>> language[3:] #获取索引0到3之外的数据 [‘python‘, ‘asp‘, ‘perl‘, ‘python‘, ‘javascript‘] >>> language[-3:] #获取后3个数据 [‘perl‘, ‘python‘, ‘javascript‘] >>> language[:5:2] #获取前5个数据中每2个数据取第一个 [‘python‘, ‘java‘, ‘asp‘] >>> language[:5:3] #获取前5个数据中每3个数据取第一个 [‘python‘, ‘python‘] >>> language[::3] #获取整个列表的数据中每3个数据取第一个 [‘python‘, ‘python‘, ‘python‘]
给列表插入数据
>>> language = [‘python‘,‘php‘,‘java‘,‘python‘,‘asp‘,‘perl‘,‘python‘,‘javascript‘] >>> language.append(‘go‘) #在列表末追加数据 >>> language [‘python‘, ‘php‘, ‘java‘, ‘python‘, ‘asp‘, ‘perl‘, ‘python‘, ‘javascript‘, ‘go‘] >>> language.insert(3,‘c#‘) #在指定的索引位置插入数据 >>> language [‘python‘, ‘php‘, ‘java‘, ‘c#‘, ‘python‘, ‘asp‘, ‘perl‘, ‘python‘, ‘javascript‘, ‘go‘]
修改列表中的数据
>>> language = [‘python‘,‘php‘,‘java‘,‘python‘,‘asp‘,‘perl‘,‘python‘,‘javascript‘] >>> language[0] = ‘c++‘ #修改指定索引位置的值 >>> language [‘c++‘, ‘php‘, ‘java‘, ‘python‘, ‘asp‘, ‘perl‘, ‘python‘, ‘javascript‘] >>> language[2:5] = ‘swift‘ #修改索引2到5的值,如果要修改的数据多于索引值,就会在索引值5之后继续添加数据,不会影响原先索引值5之后的数据 >>> language [‘c++‘, ‘php‘, ‘s‘, ‘w‘, ‘i‘, ‘f‘, ‘t‘, ‘perl‘, ‘python‘, ‘javascript‘] >>> language[2:4] = [‘swift‘,‘php‘,‘java‘,‘c‘] >>> language [‘c++‘, ‘php‘, ‘swift‘, ‘php‘, ‘java‘, ‘c‘, ‘i‘, ‘f‘, ‘t‘, ‘perl‘, ‘python‘, ‘javascript‘]
删除列表中的值
>>> language = [‘python‘,‘php‘,‘java‘,‘python‘,‘asp‘,‘perl‘,‘python‘,‘javascript‘] >>> language.pop() #删除列表最后一个元素 ‘javascript‘ >>> language [‘python‘, ‘php‘, ‘java‘, ‘python‘, ‘asp‘, ‘perl‘, ‘python‘] >>> language.remove(‘python‘) #从左往右删除第一个指定的元素 >>> language [‘php‘, ‘java‘, ‘python‘, ‘asp‘, ‘perl‘, ‘python‘] >>> del language[2:4] #根据索引删除指定的元素 >>> language [‘php‘, ‘java‘, ‘perl‘, ‘python‘]
循环列表
>>> language = [‘python‘,‘php‘,‘java‘,‘python‘,‘asp‘,‘perl‘,‘python‘,‘javascript‘] >>> for i in language: ... print(i) ... python php java python asp perl python javascript
列表排序
>>> language = [‘python‘,‘php‘,‘java‘,‘python‘,‘asp‘,‘Perl‘,‘Go‘,‘Ruby‘,‘&‘,‘python‘,‘#‘,‘%‘,‘javascript‘] >>> language.sort() #根据ascii码的顺序排列,先特殊符号,再大写,最后小写。数字不能和字符串一起进行排序 >>> language [‘#‘, ‘%‘, ‘&‘, ‘Go‘, ‘Perl‘, ‘Ruby‘, ‘asp‘, ‘java‘, ‘javascript‘, ‘php‘, ‘python‘, ‘python‘, ‘python‘] >>> language.reverse() #根据ascii码进行反向排序 >>> language [‘python‘, ‘python‘, ‘python‘, ‘php‘, ‘javascript‘, ‘java‘, ‘asp‘, ‘Ruby‘, ‘Perl‘, ‘Go‘, ‘&‘, ‘%‘, ‘#‘]
列表的拼接
>>> web = [‘html‘,‘php‘,‘asp‘,‘javascript‘] >>> language = [‘java‘,‘python‘,‘perl‘,‘go‘] >>> language + web #直接相加 [‘java‘, ‘python‘, ‘perl‘, ‘go‘, ‘html‘, ‘php‘, ‘asp‘, ‘javascript‘] >>> language.extend(web) #添加列表 >>> language [‘java‘, ‘python‘, ‘perl‘, ‘go‘, ‘html‘, ‘php‘, ‘asp‘, ‘javascript‘]
列表的复制
>>> web = [‘html‘,‘php‘,‘asp‘,‘javascript‘] >>> language = web #通过赋值来复制列表,如果原列表发生改变,复制的列表也会发生改变 >>> language [‘html‘, ‘php‘, ‘asp‘, ‘javascript‘] >>> web.append(‘python‘) >>> web [‘html‘, ‘php‘, ‘asp‘, ‘javascript‘, ‘python‘] >>> language [‘html‘, ‘php‘, ‘asp‘, ‘javascript‘, ‘python‘] >>> web = [‘html‘,‘php‘,‘asp‘,‘javascript‘] >>> language = web.copy() #深度copy,原列表发生改变,复制的列表不会发生改变,两个列表变得没有关系了 >>> language [‘html‘, ‘php‘, ‘asp‘, ‘javascript‘] >>> web.append(‘python‘) >>> web [‘html‘, ‘php‘, ‘asp‘, ‘javascript‘, ‘python‘] >>> language [‘html‘, ‘php‘, ‘asp‘, ‘javascript‘]
清空列表
>>> web = [‘html‘,‘php‘,‘asp‘,‘javascript‘] >>> web.clear() >>> web []