Home > other >  High school students zero based learning python (3)
High school students zero based learning python (3)

Time:10-24

learning python on the third day
I came again, today very tired, but still have to keep learning, nonsense not much said, Let's get started!

Today we are going to study is set and dict , they are a set of keys, but what is the difference between them, the next learning will tell us!

First of all, let's take a look at the dict

Reference
President set a task to me, I will let the result calculated from all the students, and asked to find convenient,


If only a few people, I can use the list to complete the task

 names=[' Mike ', 'Bob', 'Alice'] 
Scores=[95, 80, 85]

But the number of the school is too big, with a list of words, the longer the list, the longer the time-consuming, find up slowly,

If dict implementing, need only a "name" - "achievement" on the table, according to the name lookup results directly, no matter how much this table, can't slow search speed, written in Python a dict is as follows:
 & gt;> D={' Mike ', 95, "Bob" : 80, 'Alice' : 85} 
> [d] 'Mike'
95

Why can the dict so fast? Take the example of the dictionary, the list is not according to the phonetic radical directly to find a word a word, while the dict corresponding page number is in accordance with the phonetic radical precise query!

Use dict key-value storage (key - value), is extremely fast speed search,

Reference
the key - value store, when put in, must according to the key to calculate the value of the location, so take the time to according to the key to get the value directly,


Put the data into the dict method, in addition to the initialization time specified, also can through the key into the:
 & gt;> D [' Mike ']=90 
> [d] 'Mike'
90

Reference
with one key can only corresponds to a value, so many times for a key in the value, the value will be put in front of the value of the hedge:

 & gt;> D [' Jack ']=90 
> [d] 'Jack'
90
> D [' Jack ']=88
> [d] 'Jack'
88

Assuming that the list without the student's name, we use dict to query what could be happening?
 & gt;> D [' Ming '] 
Traceback (the most recent call last) :
The File "& lt; Stdin>" , line 1, in
KeyError: 'xiao Ming'

In order to avoid this kind of mistake, there are two solutions:
One is through the in to determine whether the key is
 & gt;> 'Ming' d in 
False

2 it is through the dict provide get () method, if the key doesn't exist, can return None , or a specified value:
 & gt;> D.g et (' Ming ') 
> D.g et (' Ming '- 1)
1

If we are not careful the wrong added a schoolmate's information, due to large quantity, not easy to find, how to do?

We can use the to delete this classmate and he is the corresponding value
 & gt;> Bob d.p op (' ') 
80
> D
{' Mike ', 95, 'Alice: 85}

please note that the internal storage of dict order has nothing to do with the key in the order of

and list the comparison, dict has the following features:
1. To find and insert extremely fast, will not slow down with the increase of the key;
2. Take up a lot of memory, memory waste,

while the list opposite
1. Time to find and insert the elements as increases;
2. To find and insert the time along with the increase with the increase of the element;
Reference
so, dict is a method of using space for time,


remember, dict key must be immutable objects , because dict is value is calculated according to the key position, if the key value will change, will lead to each computed value is different, so the dict internal chaos,

set
Reference
to create a set, there is a need to provide a list as the input collection:

 & gt;> S=set ([1, 2, 3]) 
> S
{1, 2, 3}

Here, [1, 2, 3] is a list, and {1, 2, 3} is set inside have 1, 2, 3 the three elements,

We can use the add (key) to add the elements in the set,
 & gt;> S.a dd (4) 
> S
{1, 2, 3, 4}
> S.a dd (4)
> S
{1, 2, 3, 4}

If find out? We repeat added a 4, but did not show out, the reason is that the set will automatically filter out duplicate elements,

By the remove (key) can remove elements:
 & gt;> S.r emove (4) 
> S
{1, 2, 3}

Reference
set can be seen as mathematical in the sense of a disorderly and collection without repeating elements, therefore, the intersection of two set can do math sense, and set operations such as:

 & gt;> S1=set ([1, 2, 3]) 
> S2=set ([2, 3, 4])
> S1 & amp; S2
{2, 3}
> | s1 s2
{1, 2, 3, 4}

See here, we should be clear, dict and set the only difference is that have any storage corresponding value,

However, as the principle of the set and dict , so the also not can fit into a mutable object, for it is impossible to judge whether the two variable object are equal, also cannot guarantee within the set "there won't be repeating element
"
Say so many, what is the immutable object?
The variable object, such as a list
 & gt;> A=[' c ', 'b', 'a'] 
> A.s rot ()
> A
[' a ', 'b', 'c']

For immutable objects, such as STR
 & gt;> A='ABC' 
> A.r eplace (' a ', 'a') # we will replace a a
'ABC'
> A
# 'ABC' what? There was no change?

How did this happen?

Let us use the to 'Abc' and see what will happen to
 & gt;> A='ABC' 
> B=a.r eplace (' a ', 'a')
> B
'Abc'
> A
'ABC'

So, when we use the replace () , replace is actually the role in string 'ABC' , although
  • Related