Python list method and related operation example notes

1, find the value in the index() list

>>>animals = ['cat','dog','pig']
>>>animals.index('pig')
2
>>>animals.index('cat')
0

2, append() and insert() add values ​​to the list

>>>animals = ['cat','dog','pig']
>>>animals.append('cow')
>>>animals
['cat','dog','pig','cow']
>>>animals.insert(1,'chicken')
>>>animals
['cat','chicken','dog','pig','cow']

3, count() counts the number of elements

>>>animals
['cat','chicken','dog','pig','cow']
>>>animals.append('dog')
>>>animals.count('dog')     
2

4, remove() pop() delete values ​​from the del list

>>>animals.remove('pig') #删除从左边开始找到的第一个元素
>>>animals
['cat','chicken',dog','cow']

>>>animals.pop(2) #删除并返回指定元素
['dog']
>>>animals.pop() #删除并返回最后的一个元素
['cow']
>>>animals
['cat',chicken]

>>>del animals[1]
>>>animals
['cat']

5, reverse() reversed() list reverse

>>>english = ['a','c','b']
>>>english.reverse() 		#永久反转
>>>english
['b','c','a']
>>>a = reversed(english()) 		#反转迭代器的序列值,返回的是一个把序列值经过反转之后的迭代器
>>>a
>>> <list_reverseiterator at 0x1770f45e978>
>>>print(list(a))
>>>['a', 'c', 'b']

6, sort() sorted() The list is sorted by ascii code value

Priority: Numbers>Uppercase Letters>Lowercase Letters>Chinese
Cannot sort lists that have both numbers and string values

>>>animals = ['dog','cat','pig'] 
>>>animals.sort() 					#sort()永久排序,列表变更
>>>animals
['cat','dog','pig']
>>>english 
['b','c','a']
>>>english.sort()
>>>english
['a','b','c']
>>> colour = ['Yellow','red','green','Pink']
>>> colour1=sorted(colour)
>>> colour1
['Pink', 'Yellow', 'green', 'red']
>>> colour.sort(reverse=True)  		#反向排序
>>> colour
['red', 'green', 'Yellow', 'Pink']
>>> colour.sort(key=str.lower)		#按字典顺序排序(不区分大小写)
>>> colour
['green', 'Pink', 'red', 'Yellow']

>>> list = ['a','A','1','$','中国']
>>> list1 = sorted(list) 			#sorted()临时性排序,原列表不变
>>> list1
['$', '1', 'A', 'a', '中国']
>>> list
['a', 'A', '1', '$', '中国']

7, extend() splicing two lists

>>>animals.extend(english)
>>>animals
['cat','dog','pip','a','b','c']

8, copy() deepcopy() copy list

>>> list1 = ['a','b','c']
>>> list2 = list1			#浅复制
>>> list3 = list1[:]		#深度复制
>>> list2
['a', 'b', 'c']
>>> list3
['a', 'b', 'c']
>>>list1.reverse()
>>> list1
['c', 'b', 'a']
>>> list2					#浅复制会随原列表变化而变化
['c', 'b', 'a']
>>> list3 					#深度复制与原列表互不影响
['a', 'b', 'c']

>>> list = ['a',[1,2,3],'b'] #如果无嵌套copy为深度复制
>>> list1 = list.copy()
>>> list1
['a', [1, 2, 3], 'b']
>>> list.pop()
'b'
>>> list
['a', [1, 2, 3]]
>>> list1
['a', [1, 2, 3], 'b']
>>> list[1][1] = 0 			
>>> list
['a', [1, 0, 3]]
>>> list1
['a', [1, 0, 3], 'b']

>>> import copy
>>> list2 = copy.deepcopy(list) #有嵌套的复制需copy模块的deepcopy()
>>> list
['a', [1, 2, 3]]
>>> list2
['a', [1, 2, 3]]
>>> list1[1][0] = 0
>>> print(list,list2,sep='\n')
['a', [0, 2, 3]]
['a', [1, 2, 3]]

9, Lists and operators and functions

+ 			 		组合列表,
* 					重复列表
a in list   		返回True或False
for a in list:  	迭代

len(list)  			返回长度
max(list)			返回最大值
min(list)			返回最小值
list(seq)			元祖转列表

比较操作
cmp(list1,list2)  	比较列表(python2用,python3已淘汰)
operator.lt(a, b)	需先导入operrator模块,a<b
operator.le(a, b)	a<=b
operator.eq(a, b)	a==b
operator.ne(a, b)	a!=b
operator.ge(a, b)	a>=b
operator.gt(a, b)	a>b

创建列表	

函数list()将可迭代对象(字符串或元组)转换为列表
>>> list1 =list(range(5))  #配合range()函数生成一系列数值列表(python2直接用range()即可生成)
>>> list1
[0, 1, 2, 3, 4]
>>> strA ='Hello Python'
>>> tupB = ('world','china')
>>> listA =list(strA)
>>> listB =list(tupB)
>>> print(listA,listB,sep="\n")
['H', 'e', 'l', 'l', 'o', ' ', 'P', 'y', 't', 'h', 'o', 'n']
['world', 'china']

>>> dictA = {'name':'zhangsan','age':'30'} #字典类型返回字典的键
>>> listC =list(dictA)
>>> listC
['name', 'age']

>>> listD = list(range(10,200,20))
>>> listD
[10, 30, 50, 70, 90, 110, 130, 150, 170, 190]

>>> name = 'The People Is Republic Of China'
>>> listE = name.split(' ')					#split分割字符串生成列表
>>> listE
['The', 'People', 'Is', 'Republic', 'Of', 'China']

>>> listF = [chr(x) for x in range(65,71)]	#列表推导式生成列表
>>> listF
['A', 'B', 'C', 'D', 'E', 'F']
>>> listG = [x for x in range(5,15)]
>>> listG
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

10 List slice

Format: [strat:end:step]
[:] Extract the entire string from the beginning (default position 0) to the end (default position -1)
[start:] Extract from start to the end
[:end] Extract from the beginning to end -1
[start:end] Extract from start to end-1
[start:end:step] Extract from start to end-1, extract
the position/offset of the first character from the left for every step of the character is 0, The position/offset of the last character on the right is -1

>>> list
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> list[0:]
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> list[1:]
['b', 'c', 'd', 'e', 'f', 'g']
>>> list[:-1]
['a', 'b', 'c', 'd', 'e', 'f']
>>> list[1:4]
['b', 'c', 'd']
>>> list[::-1]
['g', 'f', 'e', 'd', 'c', 'b', 'a']
>>> list[::-2]
['g', 'e', 'c', 'a']

Tuples
Tuples and lists are both a sequence, and the indexing method is the same;
tuples are formed by parentheses in English (), storing elements are the same as lists, and can be of different data types or structures; the
biggest difference from lists is, Tuples are an invariant type of data structure
. There are only two available methods for tuples: count and index (same as lists)

Expanded knowledge of the
usage of random functions in the python standard library

Function: Random generation of floating-point numbers, integers, strings, an element of a list sequence and
the use of random functions such as disrupting a set of data :

random.random()  			返回0<=x<1之间的随机实数n;
random.uniform(a,b)			上下限随机生成浮点数;
random.randint(m,n)			上下限随机生成一个整数int类型(m<=x<=n);
random.randrange(m,n,w) 	上下限间隔为w的随机整数(m<=x<n,可以只输入一个范围的值);
random.getrandbits(n)		以长整型形式返回n个随机位;
random.shuffle(seq[, random])原地指定seq序列(随机打乱);
random.choice(seq) 			从序列seq中返回随机的元素(可以是字符串,列表,元组等);
random.sample(seq, n) 		从序列seq中选择n个随机且独立的元素;

>>> list = ['a','b','c','d','e','f','g']		#随机返回列表元素
>>> print(list[random.randrange(0,len(list))])
a
>>> list[random.randrange(0,len(list))]
'e'
>>> list[random.randint(0,len(list)-1)]
'd'

Guess you like

Origin blog.csdn.net/qq_41952762/article/details/108123485