SEVEN: List

List

 

 

Description: List [] represented by square brackets, with between elements spaced apart, the type of container, the contents can be changed. List can put numbers, strings, tuples, dictionaries, lists, classes, functions, and so built

1, create a list:

list1=[]

list2=[1,2,'234',[2,3,4]]

 

2, (check) access to obtain a list of elements: the index way to get a list of the index from zero:

= List2 [1,2, ' 234 ' , [2,3,4 ]]
 Print (List2 [2 ])
 Print (List2 [. 3] [2]) 
Print (List2 [2:. 4]) # list slice acquisition 2 to the index from the index position of the element 3


Run Results:
234
4
[ '234', [2, 3, 4]]

 

3, (change) to change the list of elements: the list of elements to be reassigned

= List2 [1,2, ' 234 ' , [2,3,4 ]] 
List2 [ . 3] = [5,6,7 ]
 Print (List2) 

result: 
[ 1, 2, ' 234 ' , [5, 6, 7]]

 

4, (by) increasing list of elements: a append () and insert () method:

append () and insert () the difference:

append (element): add an element at the end, there is a change to the original list

insert (position i, the element x): added element between the two elements

= List2 [1,2, ' 234 ' , [2,3,4 ]] 
list2.append ( ' the ABC ' )
 Print (List2) 
list2.insert (. 3, 'Hello INSERT')
Print (List2)

result:
[ . 1, 2, '234', [2,. 3,. 4], 'the ABC']
[. 1, 2, '234', 'INSERT Hello', [2,. 3,. 4]]

 

5, (deleted) remove elements or list by del; list by removing remove () element; a pop () Delete elements with subscript element

 del, remove, pop difference:

remove is a list of elements to retrieve deleted; return is a list of deleted, instead of deleting elements, the return value None;

del pop and is under standard element of the list to retrieve deleted; deleted del does not return a value; pop delete return value

 

= List1 [1,2, ' 234 ' , [2,3,4 ]] 
list1.remove ( ' 234 ' ) #remove () to retrieve the elements
 Print ( " list1.remove ( '234') = " , List1 ) 
list1.pop ( 2 ) #pop () to retrieve the index of the element 
Print ( ' list1.pop (2) = ' , List1) del List1 [0] Print ( ' del List1 [0] = ' , List1) #del of the standard element to retrieve, delete will quickly

run results:
list1.remove ( '234') = [. 1, 2, [2,. 3,. 4]]
list1.pop (2) = [. 1, 2 ]
del List1 [0] = [2]

 

 6, a list of operators:

 

7, sliced ​​interception list:

= L [ ' from spam ' , ' Spam ' , ' ! SPAM ' ]
 # obtain a list of third elements: 
Print (L [2 ])
 # read penultimate element list: 
Print (L [-2 ])
 # from the second element of the list and the list taken: 
Print (L [. 1:]) 
Print (L [:])


run results:
SPAM!
Spam
[ 'Spam', 'SPAM!']
[ 'from spam', 'Spam ',' SPAM! ']

 

8, copy the list: by value and pass by reference:

Description: reference (variable variable a = b) refer to the same object operation, copy ([:] mode) means a complete copy of a new object

= Ll [ ' from spam ' , ' Spam ' , ' ! SPAM ' ] 
L2 of = Ll    # pass address 
L3 = Ll [:]     # pass value 
Print (L2 of IS Ll)    # True 
Print (L3 IS Ll)    # False

Note: Compare with is, do not use in, in a comparative element

 

 

9, a list of functions and methods:

The following functions are used for all the sequences: (SEQ strings, lists, tuples) (cmp removed comment function pyhton3)

The following list is only a function of the entry into force

 

10, operator module operator:

python3 obsolete CMP () function, it is replaced with a new function (eq, lt, le, gt, ge)

import operator             #导入运算符模块
a=4
b=9
print (operator.eq(a,b))    #等于
print (operator.lt(a,b))    #小于
print (operator.le(a,b))    #小于等于
print (operator.gt(a,b))    #大于
print (operator.ge(a,b)) #大于等于

运行结果:
False
True
True
False
False

 

 11、sort()函数:

sort() 函数用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。

语法:

list.sort(key=None,reverse=False)

  • key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
  • reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认)

 该方法没有返回值,但是对列表的对象进行排序

 

list1=[(2,3,3),(4,4,5,3,3),(4,5)]

#使用默认参数进行排序,即按元组的第一个元素来排序
list1.sort()
print (list1)

#按下标为1,即第2个元素,倒序排序
list1.sort(key=lambda x:x[1],reverse=True)
print (list1)

#先按下标为0,即第1个元素排序,如果第1个元素相同,则按下标为1,即第2个元素排倒序排序
list1.sort(key=lambda x:(x[0],x[1]),reverse=True)
print (list1)

#按元组的长度排序
list1.sort(key=lambda tup:len(tup))
print (list1)

运行结果:
[(2, 3, 3), (4, 4, 5, 3, 3), (4, 5)]
[(4, 5), (4, 4, 5, 3, 3), (2, 3, 3)]
[(4, 5), (4, 4, 5, 3, 3), (2, 3, 3)]
[(4, 5), (2, 3, 3), (4, 4, 5, 3, 3)]

 

注意:笔者从此篇开始用的是python3.7,此前是python2.7。后续会继续更新,有任何问题欢迎留言交流

 

Guess you like

Origin www.cnblogs.com/wenxiacui/p/10962086.html