Listing (list)
is one of the most commonly used data structures in Python and other languages. Python uses brackets [] to parse the list. The list is mutable (mutable)--You can change the contents of the list.
Corresponding operation:
1, check ([] slice operation) name = [' Tom ', ' Zhang San ', ' Joker ', ' John Doe ']print (name[2]) print (Name[0:3]) print (Name[0:7]) print (name[-1]) print ( Name[2:3]) print (name[0:3:1]) print (name[3:0:-1]) print (name[:])
Output:
Joker
[' Tom ', ' Zhang San ', ' Joker ']
[' Tom ', ' Zhang San ', ' Joker ', ' John Doe ']
John doe
[' Joker ']
[' Tom ', ' Zhang San ', ' Joker ']
[' John Doe ', ' Joker ', ' Zhang San ']
[' Tom ', ' Zhang San ', ' Joker ', ' John Doe ']
2. Increase (Append,insert)
name = [' Tom ', ' Zhang San ', ' Joker ', ' John Doe ']name.append (' Zhao Wu ') #默认插到最后一个位置print (name) Name.insert (1, ' Zhao Wu ') #将数据插入到任意一个位置print ( Name
Output:
[' Tom ', ' Zhang San ', ' Joker ', ' John Doe ', ' Zhao Wu ']
[' Tom ', ' Zhao Wu ', ' Zhang San ', ' Joker ', ' John Doe ', ' Zhao Wu ']
3. Change (re-assignment)
name = [' Tom ', ' Zhang San ', ' Joker ', ' John Doe ']name[3]= ' Zhao VII ' name[0:2]=[' Zhaosi ', ' Kivue ']print (name)
Output:
[' Zhaosi ', ' Kivue ', ' Joker ', ' Zhao Qi ']
4. Deletion (Remove,del,pop)
name = [' Tom ', ' Zhang San ', ' Joker ', ' John Doe ']name.remove (' Zhang San ') print (name) del name[0]print (name) del namename = [' Tom ', ' Zhang San ', ' Joker ' , ' John Doe ']name.pop () #注意, pop is a return value of Name.remove (Name[0]) print (name)
Output:
[' Tom ', ' Joker ', ' John Doe ']
[' Joker ', ' John Doe ']
[' Zhang San ', ' Joker ']
Note: Remove directly removes the list (you can also remove the slices), Del can do the slice delete or directly delete the variable, pop default delete the last element of the list (also can delete the element by the index position, the pop method is to return the delete value)
5. List built-in methods
Count method counts the number of occurrences of an element in a list
The Extend method can append multiple values from another sequence at the end of the list at once. (The Extend method modifies the list being expanded, and the original connection operation (+) does not, it returns a completely new list.) )
A = [1,2,3]b = [4,5,6]a.extend (b) print (a)
Output:
[1, 2, 3, 4, 5, 6]
The index method is used to find the indexed position of the first occurrence of a value from a list
The reverse method stores the elements in the list in reverse
The sort method is used to sort the list at the original location (not a number, sorted by ASCII table order)
Tuple (tuple)
Tuples are called read-only lists, where data can be queried but cannot be modified, so the slice operation for a list applies to tuples as well.
Tuples are written in parentheses (()), and the elements are separated by commas.
Although the elements of a tuple cannot be changed, it can contain mutable objects, such as list lists.
Constructing tuples that contain 0 or 1 elements is special, so there are some additional syntax rules:
12 |
tup1 = () # 空元组 tup2 = ( 20 ,) # 一个元素,需要在元素后添加逗号 |
Role:
1 for some data we do not want to be modified, you can use tuples;
2 In addition, the tuple's significance is that tuples can be used as keys in mappings (and members of collections)-and lists do not; tuples exist as return values for many of the built-in functions and methods.
This article is from the "on_the_road" blog, make sure to keep this source http://cqtesting.blog.51cto.com/8685091/1958808
Python Learning notes list and tuples (iii)