列表:字符串,数字,列表,元组,字典,对象
mix=[1,3.14,'小甲鱼',[7,8,9],[]] >>> mix [1, 3.14, '小甲鱼', [7, 8, 9], []]
增加列表元素:.append()、.extend() 和 .insert()
>>> mix.append['hello world'] Traceback (most recent call last): File "<pyshell#152>", line 1, in <module> mix.append['hello world'] TypeError: 'builtin_function_or_method' object is not subscriptable >>> mix.append('hello world') >>> mix [1, 3.14, '小甲鱼', [7, 8, 9], [], 'hello world'] >>> mix.extend('岁寒四友','天空') Traceback (most recent call last): File "<pyshell#155>", line 1, in <module> mix.extend('岁寒四友','天空') TypeError: extend() takes exactly one argument (2 given) >>> mix.extend(['岁寒四友','天空']) >>> mix [1, 3.14, '小甲鱼', [7, 8, 9], [], 'hello world', '岁寒四友', '天空'] >>> mix.insert(0,'北国风光') >>> mix ['北国风光', 1, 3.14, '小甲鱼', [7, 8, 9], [], 'hello world', '岁寒四友', '天空']
列表:索引、分片(slice)、拷贝、步长
mix=['北国风光', 1, 3.14, '小甲鱼', [7, 8, 9], [], 'hello world', '岁寒四友', '天空'] >>> temp=mix[0] >>> mix[0]=mix[3] >>> mix ['小甲鱼', 1, 3.14, '小甲鱼', [7, 8, 9], [], 'hello world', '岁寒四友', '天空'] >>> mix[3]=temp >>> mix ['小甲鱼', 1, 3.14, '北国风光', [7, 8, 9], [], 'hello world', '岁寒四友', '天空']
>>> mix.remove(1) >>> mix ['小甲鱼', 3.14, '北国风光', [7, 8, 9], [], 'hello world', '岁寒四友', '天空'] >>> mix.remove([]) >>> mix ['小甲鱼', 3.14, '北国风光', [7, 8, 9], 'hello world', '岁寒四友', '天空'] >>> del mix[2] >>> mix ['小甲鱼', 3.14, [7, 8, 9], 'hello world', '岁寒四友', '天空'] >>> mix.pop() '天空' >>> mix ['小甲鱼', 3.14, [7, 8, 9], 'hello world', '岁寒四友']
>>> mix[2:5] [[7, 8, 9], 'hello world', '岁寒四友'] >>> mix ['小甲鱼', 3.14, [7, 8, 9], 'hello world', '岁寒四友'] >>> mix[1:] [3.14, [7, 8, 9], 'hello world', '岁寒四友'] >>> mix[:] ['小甲鱼', 3.14, [7, 8, 9], 'hello world', '岁寒四友'] >>> mix[:4] ['小甲鱼', 3.14, [7, 8, 9], 'hello world']
列表:排序count/index(顺序.sort()和逆序.reverse())、copy() 和 clear()、推导式
>>> list1=[123,645] >>> list2=[234,543,789] >>> list3=['小甲鱼', 1, 3.14] >>> list3+'黑夜' Traceback (most recent call last): File "<pyshell#29>", line 1, in <module> list3+'黑夜' TypeError: can only concatenate list (not "str") to list >>> list3+['黑夜'] ['小甲鱼', 1, 3.14, '黑夜'] >>> list1<list2 True >>> list2>list3 Traceback (most recent call last): File "<pyshell#32>", line 1, in <module> list2>list3 TypeError: '>' not supported between instances of 'int' and 'str' >>> '小甲鱼'in list3 True
>>> list4.count('小甲鱼') 1 >>> list5.count(234) 10 >>> list4 ['小甲鱼', 1, 3.14, '黑夜'] >>> list5 [234, 543, 789, 234, 543, 789, 234, 543, 789, 234, 543, 789, 234, 543, 789, 234, 543, 789, 234, 543, 789, 234, 543, 789, 234, 543, 789, 234, 543, 789] >>> list5.index(234,4,9) 6 >>> list4.reverse() >>> list4 ['黑夜', 3.14, 1, '小甲鱼'] >>> list5.sort() >>> list5 [234, 234, 234, 234, 234, 234, 234, 234, 234, 234, 543, 543, 543, 543, 543, 543, 543, 543, 543, 543, 789, 789, 789, 789, 789, 789, 789, 789, 789, 789] >>> list5.reverse() >>> list5 [789, 789, 789, 789, 789, 789, 789, 789, 789, 789, 543, 543, 543, 543, 543, 543, 543, 543, 543, 543, 234, 234, 234, 234, 234, 234, 234, 234, 234, 234] >>> list5.sort(reverse=True) >>> list5 [789, 789, 789, 789, 789, 789, 789, 789, 789, 789, 543, 543, 543, 543, 543, 543, 543, 543, 543, 543, 234, 234, 234, 234, 234, 234, 234, 234, 234, 234]