This is the third of the Python introductory tutorials, which will specifically describe the four types of data structures commonly used in Python, which are lists, tuples, collections, dictionaries, which are often used later, so you need to understand each of these data structures.
About the line spacing problem, I looked again today, as if the code where the line spacing is already the smallest, there is no way to adjust, with mobile phone to see the words will be some big, if you want better reading effect, you can click to read the original text, browsing on the site, the effect will be better
Python list, Yuan Zu, collection, dictionary
In this article we will talk about four kinds of data structures, lists, meta ancestors, collections, dictionaries, which are commonly used in Python.
· List []
· Tuple ()
· set {}
· dict {Key:value}
Lists List
Defined
Animals = [' Tiger ', ' Bear ', ' lion ']
Add-append at end
Call the Append method to add an element to the end of the list
Animals.append (' horse ')
Animals
[' Tiger ', ' Bear ', ' lion ', ' horse ']
Index
We can access each element of the list by index, the index of the first element is 0, and the index of the last element is-1
Animals[0]
' Tiger '
ANIMALS[-1]
' Horse '
We can use Len to get the list length
Len (Animals)
4
Add –insert Anywhere
Above we said append is added to the end of the list, and we can add the insert to any location in the list.
Animals.insert (1, ' Eagle ')
Animals
[' Tiger ', ' Eagle ', ' bear ', ' lion ', ' horse ']
Delete any specified value
We use remove to remove the specified value
Animals.remove (' Tiger ')
Animals
[' Eagle ', ' bear ', ' lion ', ' horse ']
Note: If Tiger appears more than once, only the first tiger will be deleted
Delete a keyword by index
Del Animals[1]
Animals
[' Eagle ', ' lion ', ' horse ']
List reversal
We want to remember that the list is orderly, and this can be compared to other data structures, so we can reverse the ordered list
Animals.reverse ()
Animals
[' Horse ', ' lion ', ' Eagle ']
Merge of Lists
Animals.extend ([' Tiger ', ' Bear '])
Animals
[' Horse ', ' lion ', ' Eagle ', ' tiger ', ' Bear ']
Find
Animals.index (' Lion ') #返回索引
1
Animals.count (' Lion ') #返回出现次数
1
' Lion ' inanimls# return Boolean type
True
Modify
Animals[0] = ' horses '
Animals
[' Horses ', ' lion ', ' Eagle ', ' tiger ', ' Bear ']
Use of POPs
The pop returns the last element of the list, and the element is also deleted when it is returned, and if you pass in a parameter pop (i), the first element pops up
For I inrange (len (Animals)):
Animal = Animals.pop ()
Print (animal) #把整个列表倒叙输出
>
Bear
Tiger
Eagle
Lion
Horses
Animals #此时列表是空的
[]
Animals.append (' Tiger ') #我们可以往里面添加值
Animals
[' Tiger ']
List multiplication
List1 = [1,2,3]
List1*3
[1, 2, 3, 1, 2, 3, 1, 2, 3] #列表重复三次
Ganso Tuple
Ganso is a special kind of list, the difference is ganso once created cannot be modified, the actions such as sort (), append () and so on Ganso are not applicable.
When writing a program, the meta ancestor is more secure than the list, if it is read-only data, use the meta ancestor as much as possible.
Create a meta ancestor
Animals = (' Tiger ', ' lion ', ' bear ')
Attention
If you create a ganso with only one element, you need to follow the element followed by a comma
Animals = (' tiger ')
Type (animals) #可以看到这里是str类型
Str
Animals
' Tiger '
Animals = (' Tiger ',)
Type (animals)
Tuple
Animals
(' Tiger ',)
Set Set
A collection is a unordered, distinct set of data, which, first of all, is unordered, cannot be accessed using an index, and another feature is that there is no duplication of data
The collection supports some mathematical operations, such as union (and set), intersection (intersection), difference (complement)
Creation of collections
Creation of a collection using the Set function or {}, note that the creation of an empty collection can only use the Set function, and an empty dictionary is created using {}.
Animals ={' tiger ', ' Bear ', ' lion ', ' Eagle ', ' Tiger '}
Animals
{' Bear ', ' Eagle ', ' lion ', ' tiger '} #可以看到重复的元素已经被去除
Animals =set ([' Tiger ', ' Bear ', ' lion ', ' Eagle ', ' Tiger '])
Type (animals)
Set
Animals
{' Bear ', ' Eagle ', ' lion ', ' tiger '} #可以看到重复的元素已经被去除
To see if an element is in the collection
' Tiger ' in animals
True
' Tigers ' in animals
False
' Tigers ' not in animals #注意这里是 not in
True
Additions and deletions of collections
Animals.add (' horse ')
Animals
{' Bear ', ' Eagle ', ' horse ', ' lion ', ' tiger '}
Animals.remove (' horse ')
Animals
{' Bear ', ' Eagle ', ' lion ', ' tiger '}
Concept of Set Subset
<=– belongs to
Set1 ={1,2,3}
Set2 ={2,3}
Set2 Inset1
False #思考这是为什么
Set2 <=set1
True
The operation of a set
Set1 = {1,2,3,4}
Set2 ={3,4,5,6}
· | Action, there are elements in the Set1 or Set2, equivalent to the Union operation
Set1|set2
{1,2,3,4,5,6}
Set1.union (Set2)
{1,2,3,4,5,6}
· & Operations, returning elements that are in Set1 and Set2
Set1&set2
{3,4}
· -Returns elements that are not set2 in Set1
Set1-set2
{1,2}
· ^ operations, returning elements that only exist in two collections
Set1^set2
{1,2,5,6}
Dictionary Dict
A dictionary is a collection of unordered key-value pairs. Each element in the dictionary is a combination of a key and a value, and the key value must be unique in the dictionary, so it is convenient to use the key from the dictionary to get the value of its corresponding value
The creation of a dictionary
Animals = {1: ' Tiger ', 2: ' Lion ', 3: ' Horse '}
ANIMALS[1]
' Tiger '
The acquisition of value in a dictionary
If key does not exist, then Dict[key] throws an error, sometimes in order to avoid the error, we use the Get () function to obtain the value of the key, if the key does not exist, then the default returns None
Animals.get (1)
' Tiger '
Animals.get (4, ' default ')
' Default ' #key不存在的时候, return defaults
adding elements
Animals[4]= ' Eagle ' #只需要为字典中key进行赋值
Animals
{1: ' Tiger ', 2: ' Lion ', 3: ' Horse ', 4: ' Eagle '}
Delete Element
Del Animals[4]
Animals
{1: ' Tiger ', 2: ' Lion ', 3: ' Horse '}
Get keys and values
List (Animals.keys ())
[1,2,3]
List (Animals.values ())
[' Tiger ', ' lion ', ' horse ']
For Valuein animals.values ():
Print (value)
>
Tiger
Lion
Horse
Assignment and shallow copy and deep copy
col = [' Red ', ' blue ', ' green ']
Col_new =col
Col_new.append (' black ')
Col_new
[' Red ', ' blue ', ' green ', ' black ']
Col
[' Red ', ' blue ', ' green ', ' black '] #这个是赋值
Id_row =[id (ele) for Ele in Col]
Id_new =[id (ele) for ele in col_new] we can see the top two are the same
Here's a deep copy, you can look at it with a light cuff.
Importcopy
Col =[' Red ', ' blue ', ' green '
Col_new =copy.deepcopy (COL)
Col_new.append (' black ')
Col_new
[' Red ', ' blue ', ' green ', ' black ']
Col
[' Red ', ' blue ', ' green ']
We have to restudying, Python learning is to practice their own hands-on, more code to knock.