1. What is a list
A 1.1 list as a sequence (sequence) is a set of sequential elements.
The 1.2 list is the most commonly used built-in data type in Python, with brackets [element 1, Element 2 ...] Enclosed, separated by commas, there is no relationship between elements and can be of any type.
2. Declaration and access to the list
#!/usr/bin/python#-*-coding:utf-8-*-#变量的声明market = [' Apple ', ' Banana ', ' computer '] #打印列表元素print market[0],market[1], Market[2],market[-1].title () #For循环打印列表元素for element in Market:print element,element.title ()
3. Modify, add, and delete elements in the list
3.1 Modify list element, List name + index of corresponding element
#修改索引值为2, 3rd element value market[2] = ' Telephone ' Print market #打印结果: [' Apple ', ' Banana ', ' telephone ']
3.2 Add elements to the list, Python provides the append () and insert () methods, append () means adding elements at the end of the list, and insert () can specify the location of the list to add elements, such as:
Market.append (' orange ') print market #得到结果: [' Apple ', ' Banana ', ' computer ', ' orange '] market.insert (1, ' Watermelon ') print market #得到结果: [' Apple ', ' watermelon ', ' Banana ', ' computer ', ' Orange ']
3.3 Delete elements in the list, you can use the Del statement, the POP (), and the Remove () method, such as:
#删除第1个元素Apple del market[0] print market #pop () method deletes the element at the end of the list and can then use it to assign to other lists, such as: Pop_market = Market.pop () #此时把通过pop () method pops the end of the element to the new variable pop_market print type (pop_market) #通过打印pop_market的类型得知, when the type is string < Type ' str ' > #如果想让弹出的元素赋值给新的列表该怎么办呢, you can declare the list first and then append it directly with the append () method, as follows: pop_list_p = [] pop_list_p.append (Market.pop ()) Print Pop_list_p #或列表的切片, subsequent mentions pop_list = market[-1] Print market print Pop_market print Pop_list #remove () method to remove an element from the list, you can then use its value: when the #使用remove () method, you only need to make the value corresponding to the element, such as: delete element watermelon Market.remove (' watermelon ') print market
4. The list of organizations, the sort () method represents a permanent sort, the sorted () function represents a temporary sort, the reverse () method represents the reverse list element arrangement order, and the Len () function represents the list length
market = [' computer ', ' Banana ', ' Apple ']market.sort () the Print market #sort () method permanently changes the order in which the elements of the list are arranged, as a result: [' apple ', ' Banana ', ' Computer '] #sorted () function temporarily changes the order of the elements in the list. Market = [' computer ', ' Banana ', ' apple ']print (sorted) #临时性排序 [' Apple ', ' Banana ', ' computer ']print market #再次打印还是预先的顺序 [' computer ', ' Banana ', ' Apple ']market.reverse () print market #reverse () Method reverses the order of the list elements # determines the length of the list element, that is, the number of elements contained in the list, and note that the Len () function is a 1-based print len when the list element is counted
Some common operations on lists:
Use the subscript index to access the values in the list, and you can also use square brackets to intercept the characters as follows:
List1 = [' Physics ', ' Chemistry ', 1997, 2000]
List2 = [1, 2, 3, 4, 5, 6, 7]
Print ("list1[0]:", list1[0])
Print ("List2[1:5]:", List2[1:5])
Output results
List1[0]: Physics
List2[1:5]: [2, 3, 4, 5]
Update list
You can modify or update data items for a list, or you can use the Append () method to add list items
List = [' Physics ', ' Chemistry ', 1997, 2000]
Print ("Value available at index 2:")
Print (list[2])
LIST[2] = 2001
Print ("New value available at index 2:")
Print (list[2])
Output Result:
Value available at index 2:
1997
New value available at index 2:
2001
To delete a list element
Use the DEL statement to delete the elements of a list
List1 = [' Physics ', ' Chemistry ', 1997, 2000]
Print (List1)
Del List1[2]
Print ("After deleting value at index 2:")
Print (List1)
The result of the above example output:
[' Physics ', ' Chemistry ', 1997, 2000]
After deleting value at index 2:
[' Physics ', ' chemistry ', 2000]
Python List script operators
The operands of the list to + and * are similar to strings. + sign for combined list, * number for repeating list
Len ([1, 2, 3])
>>>3
[1, 2, 3] + [4, 5, 6]
>>>[1, 2, 3, 4, 5, 6]
[' hi! '] * 4
>>>[' hi! ', ' hi! ', ' hi! ', ' hi! ']
3 in [1, 2, 3]
>>>true
For x in [1, 2, 3]:
Print (x)
>>>123
Python list interception
The list of Python intercepts the type of string manipulation, as shown below
L = [' spam ', ' spam ', ' spam! ']
L[2] #读取列表中第三个元素
>>>spam!
L[-2] #读取列表中倒数第二个元素
>>>spam
L[1:] #从第二个元素开始截取列表
>>> ' Spam ', ' spam! '