Python List 基础学习

list&tuple&dict

list

list 常见操作

初始化:
list1 = [123, 'abc', 4.56, ['inner', 'list'], 7-9j]
list2 = [None, 'Something to see here']
emptylist=[]

list 位置 or index

list1 = [123, 'abc', 4.56, ['inner', 'list'], 7-9j]
正向算0 1 2 3 4, 反向过来的位置就是 -1 -2 -3 -4 -5 [反向是从-1开始算起的哟!]

list 切片:
tips: list1[start🔚pace] 索引从0开始计数,包括start,不包括end!

list1[1:4]  #['abc', 4.56, ['inner', 'list']]  
list1[3][0]	#结果是'inner'  类似于二维数组。
list1[:2] 等价于 list1[0:2:1]
list1[3:5]=['abc','xyz',345] #切片可以被赋值,元素个数可以不相同。

其他操作

list1[2]='this is list1[2]!'	#修改list 的某个值
list1.append("append element") #append 添加元素
del list1[2]

list1.remove(123)	#删除第一个遇到的 123 值。如果没有这个元素会报错:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list

list1.pop[index] #index不指定,默认是是list 最后一个元素。 ***和 remove 的区别是remove指定的是值,pop指定的是位置***
del list1	#删除整个list1

***多个 list 之间的操作 ***

list3 = ['abc', 999]
list2 = ['xyz', 789]
list2 < list3 False
两个 list 之间比较,从第0个开始比较,一旦比较出了结果就不继续后续元素的比较了。
不同类型的比较,例如 (123 < 'xyz') == True  #解释:python 核心编程P140说到:如果有一方的元素是数字,另一方最大[数字最小]

list * 2  *变成了重复操作符。

成员关系操作

list1 = [['hsp', 'wish'], 'abc', 4.56]
>>> ['hsp', 'wish'] in list1
True
>>> 'dba' in list1
False

常见函数
这里指的是 Python 解释器支持的内建函数

>>> list1
[['hsp', 'wish'], 'abc', 4.56]
>>>len(list1)
>>> max(list1),min(list1)
('abc', 4.56)
>>> list1.reverse()

enumerate 枚举元素
>>> list1
[4.56, 'abc', ['hsp', 'wish']]
>>> list(enumerate(list1))
[(0, 4.56), (1, 'abc'), (2, ['hsp', 'wish'])]
>>> for i,value in enumerate(list1):
...   print i,value
...
0 4.56
1 abc
2 ['hsp', 'wish']


zip 感觉把两个list整合在一起了,后续看下为啥??
>>> list1
[4.56, 'abc', ['hsp', 'wish']]
>>> list2
['xyz', 789]
>>> zip(list1,list2)
[(4.56, 'xyz'), ('abc', 789)]

sum  	注意:list 元素需要是数字类型
>>> a = [6, 4, 5]
>>> sum(a)
15

tuple() && list()          
tuple 和 list 之间的互换。
>>> list2
['xyz', 789]
>>> tuple2= tuple(list2)
>>> tuple2
('xyz', 789)
>>> list(tuple2)
['xyz', 789]

列表类型的内建函数 [类似于Java 的 class 的 method]

dir(list) 可以查看列表类型的内建函数。
'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'


>>> names
['wfz', 'moon', 'er0', 'info', 0, 1]
>>> names.append(range(2))            #list.append(x)
>>> names
['wfz', 'moon', 'er0', 'info', 0, 1, [0, 1]]
>>> names.pop()					#list.pop([i]) Remove the item at the given position in the list, and return it. 默认是末尾元素。
[0, 1]
>>> names
['wfz', 'moon', 'er0', 'info', 0, 1]
>>> names.index('info')    #list.index(x)  Return the index in the list of the first item whose value is x. It is an error if there is no such item.
3
>>> names.remove(0)		#移除值包含0的元素,list.remove(x)
Remove the first item from the list whose value is x.
>>> names
['wfz', 'moon', 'er0', 'info', 1]
>>> names.sort()			#sort(func=None,key=None,reverse=False) 默认的reverse 正序排序。
>>> names
[1, 'er0', 'info', 'moon', 'wfz']
>>> names.reverse()		#list.reverse()  Reverse the elements of the list, in place.
>>> names
['wfz', 'moon', 'info', 'er0', 1]
>>> names.insert(2,'th000')	# list.insert(i, x)  Insert an item at a given position.  insert 和 append 的区别是可以选择位置。
>>> names.extend(range (2))   #list.extend(L)  L 可以是一个list
>>> names
['wfz', 'moon', 'th000', 'info', 'er0', 1, 0, 1]
>>> names.count(1)	#list.count(x) Return the number of times x appears in the list.
2


判断一个元素在list 的索引位置中,先 in 判断下,再调用 index 方法
>>> list1
[4.56, 'abc', ['hsp', 'wish'], 'abc']
>>> 'abc' in list1
True
>>> list1.index('abc')
1			#注意只会返回一个位置

sorted 和 sort 区别

sorted内建函数、返回list 排序后得值,对list 的本身的值没有修改。list.sort() 是 list 本身的方法 [dir(list) 查看],会对 list 本身修改掉,不返回值。

>>> list2
['xyz', 789]
>>> sorted(list2)
[789, 'xyz']
>>> list2
['xyz', 789]
>>> list2.sort()
>>> list2
[789, 'xyz']

用list实现队列

Stack实现
Stack.put('A') == list1.append('A')
Stack.pop() == list1.pop()

Queue实现
Queque.enque('A') == list1.append('A')
Queque.deque('A') == list1.pop(0)

posted @ 2017-03-16 15:44  流水无情88  阅读(849)  评论(0编辑  收藏  举报