- 学习测试开发的Day63
- 学习时间55分钟
- 第六次全天课20190119(下午视频3H15M-4H08)
操作符函数operator
image.png
>>> a=[1]*10
>>> a
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
>>> a[0]=100
>>> a
[100, 1, 1, 1, 1, 1, 1, 1, 1, 1]
>>> a[-1]=100
>>> a
[100, 1, 1, 1, 1, 1, 1, 1, 1, 100]
>>> a[4:9]=[2,4,3,8,9]
>>> a
[100, 1, 1, 1, 2, 4, 3, 8, 9, 100]
>>>
>>> import operator
>>> operator.add(1,1)
2
>>> operator.sub(1,1)
0
>>> operator.mul(2,1)
2
>>> operator.truediv(1,2)
0.5
>>> operator.eq(1,1)
True
>>> operator.lt(1,1)
False
>>> operator.lt(2,1)
False
>>> operator.lt(1,2)
True
>>> dir(operator)
['__abs__', '__add__', '__all__', '__and__', '__builtins__', '__cached__', '__concat__', '__contains__', '__delitem__', '__doc__', '__eq__', '__file__', '__floordiv__', '__ge__', '__getitem__', '__gt__', '__iadd__', '__iand__', '__iconcat__', '__ifloordiv__', '__ilshift__', '__imatmul__', '__imod__', '__imul__', '__index__', '__inv__', '__invert__', '__ior__', '__ipow__', '__irshift__', '__isub__', '__itruediv__', '__ixor__', '__le__', '__loader__', '__lshift__', '__lt__', '__matmul__', '__mod__', '__mul__', '__name__', '__ne__', '__neg__', '__not__', '__or__', '__package__', '__pos__', '__pow__', '__rshift__', '__setitem__', '__spec__', '__sub__', '__truediv__', '__xor__', '_abs', 'abs', 'add', 'and_', 'attrgetter', 'concat', 'contains', 'countOf', 'delitem', 'eq', 'floordiv', 'ge', 'getitem', 'gt', 'iadd', 'iand', 'iconcat', 'ifloordiv', 'ilshift', 'imatmul', 'imod', 'imul', 'index', 'indexOf', 'inv', 'invert', 'ior', 'ipow', 'irshift', 'is_', 'is_not', 'isub', 'itemgetter', 'itruediv', 'ixor', 'le', 'length_hint', 'lshift', 'lt', 'matmul', 'methodcaller', 'mod', 'mul', 'ne', 'neg', 'not_', 'or_', 'pos', 'pow', 'rshift', 'setitem', 'sub', 'truediv', 'truth', 'xor']
>>>
image.png
image.png
image.png
Python列表操作函数:(仅对列表有用)
image.png
pop例子:
>>> a=list(range(10))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> a.pop()
9
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> a.pop()
8
>>> a
[0, 1, 2, 3, 4, 5, 6, 7]
>>> a.pop()
7
>>> a
[0, 1, 2, 3, 4, 5, 6]
>>>
count
>>> a.count(4)
1
extend()
>>> a.append(b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'b' is not defined
>>> b=[9,9,9]
>>> a.append(b)
>>> a
[0, 1, 2, 3, 4, 5, 6, [9, 9, 9]]
>>> a.extend(b)
>>> a
[0, 1, 2, 3, 4, 5, 6, [9, 9, 9], 9, 9, 9]
>>>
sort()和sorted()
sort会改变原有列表的值
sorted不会改变原有列表的值
>>> a=[4,3,21,-1,5]
>>> b=sorted(a)
>>> b
[-1, 3, 4, 5, 21]
>>> a
[4, 3, 21, -1, 5]
>>> b=a.sort()
>>> print(b)
None
>>> a
[-1, 3, 4, 5, 21]
>>>
reverse() 反转
>>> a
[-1, 3, 4, 5, 21]
>>> a.reverse()
>>> a
[21, 5, 4, 3, -1]
>>>
sort函数
image.png
a=[[1,2,3],[2,3,4],[-1,3,2]]
def get(L):
return L[2]
a.sort(key=get,reverse=False)
print(a)
PS D:\0grory\day6> python .\sort.py
[[-1, 3, 2], [1, 2, 3], [2, 3, 4]]
a=[[1,2,3],[2,3,4],[-1,3,2]]
def get(L):
return L[1]
a.sort(key=get,reverse=False)
print(a)
PS D:\0grory\day6> python .\sort.py
[[1, 2, 3], [2, 3, 4], [-1, 3, 2]]
a=[[1,2,3],[2,3,4],[-1,3,2]]
def get(L):
return L[1]
a.sort(key=get,reverse=True)
print(a)
PS D:\0grory\day6> python .\sort.py
[[2, 3, 4], [-1, 3, 2], [1, 2, 3]]
使用元组的最后一个元素大小比较来实现 list 的排序:
list1 = [(3,5,3),(5,3,6,3),(1,1,2,4,5,6),(2,9)]
def L(tup) :
return tup[-1]
list1.sort(key = L,reverse = True)
print (list1)
自动生成列表
生成递增列表:
>>> list1 = list(range(10))
>>> list1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list2 = list(range(1, 10))
>>> list2
[1, 2, 3, 4, 5, 6, 7, 8, 9]
加步长生成递增列表:
>>> list3 = list(range(1, 10, 2))
>>> list3
[1, 3, 5, 7, 9]
>>> range(0,10)
range(0, 10)
>>> list(range(10)
... )
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> for i in range(10):print(i,"")
...
0
1
2
3
4
5
6
7
8
9
>>> for i in list(range(10)):print(i)
...
0
1
2
3
4
5
6
7
8
9
>>>
列表复制
image.png
引用复制
>>> a=[1,2,3,4]
>>> a
[1, 2, 3, 4]
>>> b=a
>>> b
[1, 2, 3, 4]
>>> a.remove(1)
>>> b
[2, 3, 4]
>>> id(a)
2584104191560
>>> id(b)
2584104191560
>>>
非引用复制
>>> a=[1,2,3,4]
>>> b=a[:]
>>> a
[1, 2, 3, 4]
>>> b
[1, 2, 3, 4]
>>> a.remove(1)
>>> a
[2, 3, 4]
>>> b
[1, 2, 3, 4]
>>>
image.png
增删改查遍历的综合操作
代码示例:
#encoding=utf-8
booklist=[u'selenium Webdriver 实战宝典','thinking in java',u'异类','how
google test software',u'探索式测试']
#计算列表的长度
print ('你一共有',len(booklist),'本书')
print ('我所有的书名是:')
#遍历列表
for book in booklist:
print (book)
print ('新买了一本书《python cookbook》')
#列表中增加元素
booklist.append('python cookbook')
#打印列表对象
print ('我当前的书:',booklist)
#列表排序
booklist.sort()
print ("排序后的书目列表:",booklist)
#查找列表的指定元素
print ("我的第一本书:",booklist[0])
print ("我的最后一本书:",booklist[len(booklist)-1])
#删除某个列表元素
del booklist[0]
print ("卖掉我的第一本书后,我的数据列表是:",booklist)
#修改某个列表元素
booklist[len(booklist)-1]='Testng cookbook'
print ("修正我的最后一本书的书名:",booklist[len(booklist)-1])
列表的排序
代码示例:
x=[4,6,2,1,7,9,4]
y=x[:]
y.sort()
print (x)
print (y)
执行结果:
[4, 6, 2, 1, 7, 9, 4]
[1, 2, 4, 4, 6, 7, 9]
综合列表(列表推导)
image.png
推导列表
>>> a=[i for i in range(10) if i%2==0]
>>> a
[0, 2, 4, 6, 8]
>>>
i for i in range(10) if i%2==0
等价于
a=[]
for i in range(10):
if i%2==0:
a.append(i)
print(a)
只写一个for也可以
>>> a=[i for i in range(5)]
>>> a
[0, 1, 2, 3, 4]
>>>
放表达式也可以
>>> a=[i*i for i in range(5)]
>>> a
[0, 1, 4, 9, 16]
>>>