list的用法(列表)
1.list函数可以把字符串切割成list
In [2]: arr = list('python')
In [3]: arr
Out[3]: ['p', 'y', 't', 'h', 'o', 'n']
2.遍历list
In [7]: for i in ['a','b','c']:
...: print(i)
...:
a
b
c
3.长度,最大值,最小值
In [12]: arr = [1,3,5,-2,-8,357]
In [13]: print(len(arr))
6
In [14]: print(max(arr))
357
In [15]: print(min(arr))
-8
4.del删除list中的元素
In [17]: arr
Out[17]: [1, 3, 5, -2, -8, 357]
In [18]: arr = ['c++','c','java','html']
In [19]: del arr[2]
In [20]: print(arr)
['c++', 'c', 'html']
5.list可以做乘法
In [8]: arr = ['js','python','css','html']
In [9]: arr * 2
Out[9]: ['js', 'python', 'css', 'html', 'js', 'python', 'css', 'html']
In [10]: arr1 = ['mon','tus']
In [11]: arr + arr1
Out[11]: ['js', 'python', 'css', 'html', 'mon', 'tus'
6.修改值和切片
In [21]: arr= ['c','python','css','js','html','node']
In [22]: arr[0]
Out[22]: 'c'
In [23]: arr[-1]
Out[23]: 'node'
In [24]: arr[1]
Out[24]: 'python'
In [25]: arr[-3]
Out[25]: 'js'
7.切片---获取list中一部分连续的数据
>>>arr= ['c','python','css','js','html','node']
>>>print arr[1:3]
['python','css']
>>>print arr[0:1]
['c']
>>>print arr[-1:-3]
[]
>>>print arr[-3:-1]
['js','html']In [26]: arr= ['c','python','css','js','html','node']In [27]: print(arr[1:3])['python', 'css']In [28]: print(arr[0:1])['c']In [29]: print(arr[-1:-4])[]In [30]: print(arr[-1:-3])[]In [31]: print(arr[-3:-1])['js', 'html']In [32]: print(arr[3:]) ['js', 'html', 'node']In [33]: print(arr[::])['c', 'python', 'css', 'js', 'html', 'node']In [34]: print(arr[:3])['c', 'python', 'css']
>>>print arr[-3:]
['js','html', 'node']
>>>print arr[3:]
['js','html', 'node']
>>>print arr[:3]
['c','python', 'css']
>>>print arr[:]
['c','python', 'css', 'js', 'html', 'node']
8.切片可以赋值
In [35]: arr
Out[35]: ['c', 'python', 'css', 'js', 'html', 'node']
In [36]: arr[2:4]=['pc','wd']
In [37]: arr
Out[37]: ['c', 'python', 'pc', 'wd', 'html', 'node']
9.切片的赋值功能可以插模拟入和删除元素
In [39]: arr
Out[39]: ['c', 'python', 'pc', 'wd', 'html', 'node']
In [40]: arr[1:1]= ['pc','wd']
In [41]: arr
Out[41]: ['c', 'pc', 'wd', 'python', 'pc', 'wd', 'html', 'node']
In [42]: arr[2:3]= []
In [43]: arr
Out[43]: ['c', 'pc', 'python', 'pc', 'wd', 'html', 'node']
10.append 向list 最后追加元素
In [44]: arr= ['wd','woniu','lp']
In [45]: arr.append('suiji')
In [46]: arr
Out[46]: ['wd', 'woniu', 'lp', 'suiji']
11.count统计list中某个元素出现的次数
In [47]: arr= [2,3,4,5,6,7,8,9,43,24,234,423,423,12,2,3,4,52,2]
In [48]: arr.count(2)
Out[48]: 3
12.冒泡排序
In [49]: arr= [1,2,3,123,11,123,421,124,2,3,4,5,6]
In [50]: for i in range(len(arr)-1):
....: for l in range(len(arr)-1-i):
....: if arr[l]>arr[l+1]:
....: arr[l],arr[l+1]=arr[l+1],arr[l]
....:
In [51]: arr
Out[51]: [1, 2, 2, 3, 3, 4, 5, 6, 11, 123, 123, 124, 421]
13.extend扩展源列表(修改了原列表,没有返回值)
In [53]: b = [7,8,9,2]
In [54]: a = [1,2,3]
In [55]: b = [4,5,6]
In [56]: a.extend(b)
In [57]: b.extend(a)
In [58]: a
Out[58]: [1, 2, 3, 4, 5, 6]
In [59]: b
Out[59]: [4, 5, 6, 1, 2, 3, 4, 5, 6]
14.index从列表中找出某个值,返回第一个匹配项的索引位置
In [60]: arr= [2,'b',3,'c',2,4,7]
In [61]: arr.index('b')
Out[61]: 1
In [62]: arr.index('c')
Out[62]: 3
15.insert插队arr.insert(位置,插入的内容) 突然来了一个美女,要插队,大家都不好意思拒绝
In [63]: arr= [1,2,3,4,5,6,7]
In [64]: arr.insert(3,'four')
In [65]: arr
Out[65]: [1, 2, 3, 'four', 4, 5, 6, 7]
16.pop根据索引移除list中的元素,并且返回,默认删除最后一个(不传参数的话,和append正好相反,pop是唯一一个即修改了源列表,又有返回值的方法)
In [66]: arr= [1,2,3,4]
In [67]: arr.pop()
Out[67]: 4
In [68]: arr
Out[68]: [1, 2, 3]
In [69]: arr= [1,2,3,4]
In [70]: arr.pop(2)
Out[70]: 3
In [71]: arr
Out[71]: [1, 2, 4]
17.remove 根据值来删除元素,删除第一个匹配项(如果没有匹配项,报错)
In [74]: arr= ['a','b','c','d']
In [75]: arr.remove('a')
In [76]: arr
Out[76]: ['b', 'c', 'd']
#remove如果没有匹配到则抛出ValueError的异常
In [77]: arr.remove('asdf')-----------------------------------
ValueError: list.remove(x): x not in list
18.reverse LIST 反向修改列表,不返回值
In [78]: arr= [1,2,3,4]
In [79]: arr.reverse()
In [80]: arr
Out[80]: [4, 3, 2, 1]
19.sort方法用于排序,修改list本身
In [81]: arr= [-11,2,1,5]
In [82]: arr.sort()
In [83]: arr
Out[83]: [-11, 1, 2, 5]
dict的一些方法