6. Lists [List]
In Python, the list is represented by [], and the intermediate elements can be of any type, separated by commas. The list is a mutable type.
Common actions for lists:
In the list I think the more important is the addition and deletion of the change, there are some similar to the operation of the string;
Definition list: QQ = [1,2,3,4, ' =====>look ', ' QQ '] This list will grow like this
Add: Append (), insert () method
1.append () Method: Adds an element #列表名 at the end. Append (Element)
2.insert () Method: Adds an element or list #列表名 at the specified location. Insert (index, Element)
Delete: Del,pop (), remove (), clear ()
1.del: You can delete the specified value #del list name ===> Delete the list, and delete the fixed value if the index is added
2.pop () Method: #默认从列表的末尾删除, you can also delete the specified value
3.remove () Method: Deletes the specified value
4.clear () Method: Empty Data
Change:
1. Direct modification by positioning to subscript
Search: The query list can be labeled and sliced
(The list value is the default forward value, but can also reverse the value, here do not write, you have to test the image is more profound----the end of the parameter to 1 (but this parameter can not write, but it became a positive (this is just with a friend, he mentioned that I have to add the way)))
1. Subscript value, starting from 0
2. Slice: Gu Tou regardless of tail, and slice subscript operation is also used for string
Other actions for the list:
Index (): Gets the subscript for the specified element
Count (): Gets the number of occurrences of the specified element
Extend (): Merge two lists, modify the values of the original list, only merge to the end of the original list
Sort (): Sort, default is ascending, want to descending you need to modify default parameter reverse (FLIP) True
The difference between insert and extend:
1.insert can specify the position of the inserted element, extend cannot be specified, can only be merged to the end
2.insert is the addition of elements on the basis of the original list, everything can be added (list, string ...). ), extend can only merge 2 lists, extend the list, and not add a string
This blog is in accordance with the blog I wrote in the Diary of the summary, examples of what did not write, but I have personally tested, there is inevitably part of the not to write the whole, many forgive ___ (this typesetting I always get bad, I will slowly correct, but I think as long as useful, typesetting this is not a problem, After all, it's not as good as it looks.
This article is from the "aqq_1024" blog, make sure to keep this source http://52770825.blog.51cto.com/13217087/1957788
Python basic data type __ List