1. indexing and slicing list
Index list
Index and a list of strings like this:
lst = [ "twist vine", "Wang Jianlin", "Yun Ma", "Zhou medical", "Heung"]
lst[0] lst[1] lst[2]
However, the list of strings is a list of different places that can change the
Such as: lst [3] = "boss" # do this step in the list of operating illegal but legitimate operation in the string
Slice list
Slice of the list is similar to the string
lst = [ "twist vine", "Wang Jianlin", "Yun Ma", "Zhou medical", "Heung"]
lst [2: 5] # give [ "Yun Ma", "Zhou medical", "Heung"]
LST [: 3] # get [ "twist vine", "Wang Jianlin," "Ma Yun"]
LST [. 1:: 2] # give [ "Wang Jianlin", Zhou doctor "]
LST [2:: -1] # give [ "Yun Ma", "Wang Jianlin", "twist vine"]
2. The list of CRUD
Increasing list
1) lst.append ( "To add an element") # add elements in brackets at the end of the list
2) lst.insert (inserted location, "insert element") # insert elements within parentheses in the specified list index, the original element is moved rearwardly one
3) lst.extend () # recursive addition, attention is iterative
Example: lst = [ "Wang Zhiwen", "Zhang ⼭", "Oliver infinite"]
lst.extend ([ "twist vine", "twist hurt"])
#lst = [ "Wang Zhiwen," "Zhang ⼭," "Oliver knows no boundaries," "twist vine", "twist hurt."]
lst.extend("abc")
#lst = [ "Wang Zhiwen", "Zhang ⼭", "infinite Oliver", "a", "b", "c"]
4) a list of the merger:
lst1 = [ "twist vine", "Wang Jianlin", "Ma Yun"], lst2 = [ "Zhou medical", "Heung"]
lst1 + lst2 = [ "twist vine", "Wang Jianlin", "Yun Ma", "Zhou medical", "Heung"]
Delete list
1) lst.pop ( "number a few elements") # to delete the list and remove the elements within the brackets
lst.pop () # default the last element in the list
2) lst.remove ( "Delete element")
# Delete the specified elements in parentheses, if only the same name, delete the first (from left to right as scanning)
Note: When you remove () to delete the specified element if there is no list will be given!
3) lst.clear () # clear the list (gray often violent)
4) del LST [Start: End] # sections deleted (less violent)
Modify the list of
lst = [ "Bai", "too dark", "colored", "Silver King", "day day"]
1) direct access to a list of elements to be modified
lst [1] = "very dirty" # 1, the element is modified to be too dirty
2) the list of slices can also be modified
lst [1: 4] = [ " Submenu shing a ⻳ reach of children"] # give [ 'Bai', 'shing a Submenu ⻳ reach of children', 'day days']
3) also took steps to modify the list
lst [1: 4: 3] = [ "twist vine", "my only"]
# Note here that if the step size with time slice, attention should be modified when the number of elements corresponding to the same
Should read: LST [. 1:. 4:. 3] = [ "twist vine"]
LST [0:. 4: 2] = "A" # 0,2 because two points slices, only one assignment, an error is reported
LST [0:. 4: 2] = "ab &" # 0,2 because two points slices, there is an assignment, but may be changed by the iteration 2, can be
LST [0:. 4: 2] = "ABC" # 0,2 because two points slices, there are three assignments, 3 can become an iterative, and therefore not
Alternatively only string element 4) a list of calls replace () method can not replace an int
. 5) str.join (SEQ) # of elements in the sequence to specify the character string into a new connection
str = "-"; seq = ("a", "b", "c"); # 字符串序列 print str.join( seq ); #"a_b_c"
其他操作
1) lst.count("关键字") #查询关键字出现的个数
2) lst.sort() #排序,默认升序
lst.sort(reverse = True) #降序
3)lst.reverse() #颠倒列表的顺序
3.元组
元组: 俗称不可变的列表.又被称为只读列表, 元组也是python的基本数据类型 之⼀, 用小括号括起来, 里面可以放任何数据类型的数据, 查询可以. 循环也可以. 切片也可以. 但就是不能改.
tu = (1, "太白", "李白", [], "怎么黑")
注意: 这里元组的不可变的意思是子元素不可变. 而子元素内部的子元素是可 以变, 这取决于子元素是否是可变对象.
例:tu[3].append("你好啊") #这句话就合法,因为元组的子元素是列表,而列表可改
注:元组中如果只有一个元素. 一定要添加一个逗号, 否则就不是元组
例: tu = (1,) #这是最低限度的元组,如果没有逗号则数据是数据本身
tu = (1) #这是int类型
tu = ("a") #这是str类型
tu =() #这是元组本身
4.range()
range(start : end : step) #能够创建一个数字列表,语法与切片类似,但是步长负数与正 数表达一致
例:range(10) #就是创建一个(0,1,2,3,4,5,6,7,8,9)的列表
range(0:20:5) #(0,5,10,15)
range(0:-10:-2) #(0,-2,-4,-6,-8),无法倒着取数,这是与字符串和列表不同的地方