#!/usr/bin/env Python3
#-*-Coding:utf-8-*-
#Author: Jam
#0, List
‘‘‘
The sequence is the most basic data structure, each element in the sequence is assigned a number, its position index first is 0, the second is 1, and so on
The list is the most commonly used Python data type, which appears as a comma-separated value within a square bracket
Data items for a list do not need to have the same type
‘‘‘
#1, create a list
List1 = [' Google ',' Runoob ',1997,2000]
List2 = [1,2,3,4,5]
LIST3 = ["A","B","C","D","E"]
#print (List1, ' \ n ', List2, ' \ n ', list3) #3行一起打印, use \ n to wrap
Print‘‘‘
%s
%s
%s
"% (LIST1,LIST2,LIST3))#3行一起打印, use ' ' ' symbol
#输出结果:
‘‘‘
[' Google ', ' Runoob ', 1997, 2000]
[1, 2, 3, 4, 5]
[' A ', ' B ', ' C ', ' d ', ' e ']
‘‘‘
#2, accessing values in a list
#和字符串一样, the list index starts at 0 and can be intercepted, combined, and so on.
List1 = [' Google ',' Runoob ',1997,2000]
Print"List1[0]:", list1[0])# Use subscript to access the values in the list
Print"List1[1:4]", list1[1:4])#列表可以使用方括号的形式截取字符
#输出结果:
‘‘‘
List1[0]: Google
List1[1:4] [' Runoob ', 1997, 2000]
‘‘‘
#3, update list
#可以对列表的数据进行修改或更新, you can also use the Append () method to add a list item:
List1 = [' Google ',' Runoob ',1997,2000]
Print"The third element of the list is:", list1[2])
list1[2] =2001
Print"The third element of the updated list is:", list1[2])
‘‘‘
The third element of the list listing is: 1997
The third element of the updated list is: 2001
‘‘‘
#4, delete list elements
#可以使用del to remove elements from a list
List1 = [' Google ',' Runoob ',1997,2000]
Print"List:", List1)
Del list1[2]
Print"Delete the third element of the list listing:", List1)
#输出结果:
‘‘‘
List: [' Google ', ' Runoob ', 1997, 2000]
Delete the third element of the list: [' Google ', ' Runoob ', 2000]
‘‘‘
#5, List script operators
#列表对 + and * operators are similar to strings. + sign for combined list, * number for repeating list
"is shown below:
Python Expression Result Description
Len ([1, 2, 3]) 3 length
[1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] combination
[' hi! '] * 4 [' hi! ', ' hi! ', ' hi! ', ' hi! '] repeat
3 in [1, 2, 3] True whether the element exists in the list
For x in [1, 2, 3]: print (x) 1 2 3 iterations
‘‘‘
#输出结果
"The following is practiced in the Python interaction:
>>> Len ([+])
3
>>> [4,5,6] + [+]
[1, 2, 3, 4, 5, 6]
>>> [' hi! '] * 4
[' hi! ', ' hi! ', ' hi! ', ' hi! ']
>>> 3 in [+]
True
>>> for x in [All-in-A-Z]: print (x)
...
1
2
3
‘‘‘
#6, List interception
#列表的截取与字符串操作类型 as follows:
l=[' Google ',' Runoob ',' Taobao ']
#操作:
‘‘‘
Python Expression Result Description
L[2] ' Taobao ' reads a third element
L[-2] ' Runoob ' reads the second-to-last element from the left: count from the right
L[1:] [' Runoob ', ' Taobao '] outputs all elements after the start of the second element
‘‘‘
#练习输出:
‘‘‘
>>> L = [' Google ', ' Runoob ', ' Taobao ']
>>> L[2]
' Taobao '
>>> L[-2]
' Runoob '
>>> l[1:]
[' Runoob ', ' Taobao ']
>>>
‘‘‘
#7, List stitching
‘‘‘
>>> squares = [1,4,9,16,25]
>>> squares + [36,49,64,81,100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
‘‘‘
#8, List nesting
#使用列表嵌套即在列表里面创建其它列表
‘‘‘
>>> a = [' A ', ' B ', ' C ']
>>> n = [+ +]
>>> x = [A,n]
>>> x
[[' A ', ' B ', ' C '], [1, 2, 3]
>>> X[0]
[' A ', ' B ', ' C ']
>>> X[1]
[1, 2, 3]
>>> X[0][1]
' B '
>>> X[1][1]
2
‘‘‘
#9, Python list functions
‘‘‘
ordinal function
1 len (list)
Number of list elements
2 Max (list)
Returns the maximum value of a list element
3 min (list)
Returns the minimum value of a list element
4 list (SEQ)
Convert a tuple to a list
‘‘‘
#9.1, Len () method
#描述: Len () method returns the number of list elements
#语法: Len (list)
#参数: List is a listing of the number of elements to calculate
#返回值: Returns the number of list elements
#实例:
#以下这一段测试在pycharm里面没有输出, at first don't know what the reason is, found that the code in the Python interface is normal, and later found to go up in the code used
#list命名的变量, list in the interpreter is a module of things, variable name to try to avoid these with the function name, method name overlap name.
List1 = [' Google ',' Runoob ',' Taobao ']print (len (list1)) #输出: 3list2=list (Range (5)) # Create a 0-4 list of print (Len (list2)) #输出: 5print (LIST2) #输出: [0, 1, 2, 3, 4 ] #python交互器输出结果: ' >>> list1 = [' Google ', ' Runoob ', ' Taobao ']>>> print (len (list1)) 3>>> List2=list (Range (5)) >>> print (len (list2)) # Create a list of 0-4 5>>> print (LIST2) [0, 1, 2, 3, 4] "#9.2, Max () Method # Description: The max () method returns the maximum value in the list element # syntax: Max (list) #参数: List is the listing to return the maximum value of # return value: Returns the maximum value in a list element # instance: list1,list2 = [' Google ', ' Runoob ', ' Taobao '],[456,700,200]print ("List1 max element value:", Max (List1)) print ("List2 maximum element value:", Max (List2)) #输出结果: #list1最大元素值: taobao# List2 Maximum element value: 700#9.3, Min () method # Description: Min () method returns the minimum value in a list element # syntax: min (list) #参数: List is the listing to return the minimum value # return value: Returns the minimum value in a list element # instance: List1,list2 = [' Google ', ' Runoob ', ' Taobao '],[456,700,200]print ("List1 min. Element value:", min (list1)) print ("List2 min. Element value:", min (list2)) # Output: #list1最小元素值: Google#list2 Minimum element value: 200#9.4, List () method # Description: The list () method is used to convert tuples to lists # Note: Tuples are very similar to lists, except that the tuple's element values cannot be modified, and tuples are placed in parentheses. The list is placed in square brackets. #语法: List (seq) #参数: List is the tuple to convert to List # return value: Return List # instance: Atuple = (123, ' Google ', ' Runoob ', ' Taobao 'List1 = List (atuple) print (' list element: ', List1) #结果输出: #列表元素: [123, ' Google ', ' Runoob ', ' Taobao '] ' interactive practice:>>> atuple = (123, ' Google ', ' Runoob ', ' Taobao ') >>> List1 = List (atuple) >>> print (' list element: ', List1) list element: [123, ' Google ', ' Runoob ', ' Taobao ']>>> type (list1) <class ' list ' >>>> type (atuple) <class ' tuple ' >>>> "str =" Hello World "LIST2 = List (str) print (' list element: ', List2) #结果输出: #列表元素: [' H ', ' e ', ' l ', ' l ', ' o ', ' ', ' W ' , ' O ', ' r ', ' L ', ' d '] ' interaction practice:>>> str = "Hello World" >>> list2 = List (str) >>> print (' list element: ', lis T2) List elements: [' H ', ' e ', ' l ', ' l ', ' o ', ' ', ' W ', ' o ', ' r ', ' L ', ' d ']>>> type (str) <class ' str ' >>>> type (list2) <class ' list ' >>>> ' #10, Python list method #10.1, List.append (obj) #描述: Add a new object at the end of the list # syntax: List.append ( obj) #参数: obj--The object # return value added to the end of the list: The method has no return value, but modifies the original list # instance: List1 = [' Google ', ' Runoob ', ' Taobao ']list1.append (' Baidu ') print ("Updated list:", List1) #输出: #更新后的列表: [' Google ', ' Runoob ', ' Taobao ', ' Baidu '] #10.2, List.count (obj) #描述: The Count () method is used to count the number of occurrences of an element in the list # syntax: List.count (obj) #参数: An object in the obj--list # return value: Returns the number of occurrences of an element in the list # Example: Alist = [123, ' Google ', ' Runoob ', ' Taobao ', 123]print ("Number of 123 elements:", Alist.count (123)) print ("Number of Runoob elements:", Alist.count (' Runoob ')) #结果输出: Number of #123 elements: number of 2#runoob elements: 1#10.3, list.extend (seq) #描述: Extend () function is used to append multiple values from another sequence at the end of the list (extending the original list with a new list) #语法: List.extend (seq) #参数: seq--element List # return value: The method does not return a value, but adds a new list content to the list that already exists # Example: List1 = [' Google ', ' Runoob ', ' Taobao ']list2 = List (range (5)) List1.extend (list2) Print ("Extended list:", List1) #结果输出: # Expanded list: [' Google ', ' Runoob ', ' Taobao ', 0, 1, 2, 3, 4] #10.4, List.index (obj) #描述: the index () function is used to find the first matching index position of a value from a list # Syntax: List.index (obj) #参数: obj--Find Object # Return Value: This method returns the index position of the lookup object and throws an exception if no object is found # instance: list1 = [' Google ', ' Runoob ', ' Taobao ']print (' The Runoob index value is: ', List1.index (' Runoob ')) print (' Taobao index value: ', List1.index (' Taobao ')) #结果输出: #Runoob index Value: 1#taobao index value: 10.5, List.insert () #描述: the Insert () function is used to insert the specified object into the specified position of the list # syntax: List.insert (index,obj) #参数: #index--the index position at which the object obj needs to be inserted # obj--object to insert List # return value: The method does not return a value, but inserts an object # instance at the specified position in the list: List1 = [' Google ', ' runoob ', ' Taobao ']list1.insert (1, ' Baidu ') print ("After the list insert element:", List1) #结果输出: #列表插入元素后为: [' Google ', ' Baidu ', ' Runoob ', ' Taobao '] #10.6, List.pop () #描述: the pop () function removes an element from the list (the last element by default), and returns the value of the element # syntax: List.pop (obj=list[-1] ) #参数: # obj--optional parameter, to remove the object of the list element # return value: The method returns the element object that was removed from the list # instance: List1 = [' Google ', ' Runoob ', ' Taobao ']list1.pop () ' Print ' (' column represented in: ') , List1) List1.pop (1) Print (' column represented in: ', List1) #结果输出: #列表现在为: [' Google ', ' Runoob '] #列表现在为: [' Google '] #10.7, List.remove () # Description: The Remove () function is used to remove the first occurrence of a value in the list # syntax: List.remove (obj) #参数: # obj--object to remove in List # return value: The method does not return a value, but removes the first occurrence of a value in the list. #实例: List1 = [' Google ', ' Runoob ', ' Taobao ', ' Baidu ']list1.remove (' Taobao ') print ("column behaves as:", List1) list1.remove (' Baidu ') Print ("column behaves as:", List1) #结果输出: #列表现在为: [' Google ', ' runoob ', ' Baidu '] #列表现在为: [' Google ', ' Runoob '] #10.8, List.reverse () # Description: Reverse () is used to reverse the elements in the list # syntax: List.reverse () #参数: #N/a=not Available (unnecessary) #返回值: The method does not return a value, but it reverses the order of the Elements of the list # instance: List1 = [' Google ', ' Runoob ', ' Taobao ', ' Baidu ']list1.reverse () print ("After list reversal:", List1) #结果输出: #列表反转以后: [' Baidu ', ' Taobao ', ' RunoOB ', ' Google '] #10.9, List.sort ([]) #描述: the sort () function is used to sort the original list, and if the parameter is specified, the comparison function specified by the comparison function is used. #语法: List.sort ([func]) #参数: #fune--optional parameter, if specified this parameter is used to sort the method of the parameter # return value: The method does not return a value, but it sorts the objects of the list # instance: List1 = [' Google ', ' Runoob ', ' Taobao ', ' Baidu ']list1.sort () print ("Sorted list:", List1) #结果输出: #列表排序后: [' Baidu ', ' Google ', ' Runoob ', ' Taobao ']# 10.10, List.clear () #描述: the Clear () function is used to empty the list, similar to Del a[:] #语法: List.clear () #参数: No # return Value: The method has no return value # instance: list1 = [' Google ', ' Runoob ' , ' Taobao ', ' Baidu ']list1.clear () print ("After the list is emptied:", List1) #结果输出: #列表清空后: [] #10.11, List.copy () #描述: Copy () for copying lists, similar to a[:]# Syntax: List.copy () #参数: No # return value: Returns the new list after replication # instance: list1 = [' Google ', ' Runoob ', ' Taobao ', ' Baidu ']list2 = List1.copy () print (" List2 list: ", list2) List3 = List1[:]print (" list3 list: ", List3) #结果输出: #list2列表: [' Google ', ' Runoob ', ' Taobao ', ' Baidu ']# LIST3 list: [' Google ', ' Runoob ', ' Taobao ', ' Baidu ']
Python path second article: Python Basics (1)-list