Python Learning notes (3)

Source: Internet
Author: User

important data Types Dict and set
Dict Find value by key (key and Value Association)
The curly braces {} indicate that this is a dict, and then follow the Key:value and write them out. The last one key:value is good to omit.

Note: A single-element tuple must be appended with a comma
Dict the last comma can be omitted
Since Dict is also a collection, the Len () function can calculate the size of any collection

>>len (d)
3 Note: One key-value is counted, so the dict size is 3
Visit dict
Can simply use the form of D[key] to find the corresponding value, which is similar to the list, the difference is that the list must use the index to return the corresponding element, and dict directly using key (through key to access the value of Dict, as long as the key exists, Dict return the corresponding value, if key does not exist, will be directly error: KEYERROR)

Features of Dict:
Knowledge Point: Dict Find Fast. The list lookup rate decreases as the element increases.
Cons: Large memory footprint. List slow but memory footprint is small
The first feature of Dict is that the search speed is fast, regardless of whether the dict has 10 elements or 100,000 elements, the search speed is the same. The search speed of the list decreases as the element increases.
However, the dict search speed is not without cost, dict the disadvantage is that the memory is large, but also waste a lot of content, and the list is the opposite, the use of small memory, but the search speed is slow
The second feature of Dict is that the stored key-value sequence is not in order! This is different from list (Dict is found by key, so in a dict, key cannot be duplicated)
For example:
b==
' Adam ': 95,
' Lisa ': 85,
' Bart ': 59
}
>>print D
{' Lisa ': $, ' Adam ': Up, ' Bart ': 59}
The order of printing is not necessarily the order in which we are created, and the order in which different machines are printed may be different, which means: The dict interior is unordered and cannot be stored in an ordered set with Dict
The third feature of Dict is that as the element of key Nowe rain is immutable, Python's basic types such as strings, integers, floating-point numbers are immutable, can be used as key. But the list is mutable and cannot be a key
Immutable this limit is only used for key,value whether the variable does not matter

Update Dict
Dict is mutable, that is, we can add new Key-value in the King Dict at any time. For example:
>>d[' Paul ']=72
>>print D
{' Lisa ': $, ' Paul ': $, ' Adam ': Up, ' Bart ': 59}
If the key already exists, the copy will replace the original value with the value of the heart.
>>d[' Bart ']=60
>>print D
{' Lisa ': $, ' Paul ': $, ' Adam ': Up, ' Bart ': 60}
Traverse Dict
Since Dict is also a collection, traversing the dict is similar to traversing the list, and can be implemented through a for loop
Use the For loop directly to facilitate dict key
>>d={' Lisa ': $, ' Adam ': Up, ' Bart ': 59}
>>for Key in D:
... print Key
...
Adam
Lisa
Bart
Because the corresponding value can be obtained by key, the value can be obtained in the loop body.
d={
' Adam ': 95,
' Lisa ': 85,
' Bart ': 59
}
For key in D:
Print key+ ":", D[key]
Operation Result:
Lisa:85
Adma:95
bart:59

Set
Dict's role is to establish a set of keys and a set of value mappings, Dict key is not repeatable
The set holds a series of elements, which is much like the list, but the set elements are not duplicates and are unordered, which is like the Dict key.
The way to create a set is to call set () and pass in a list,list element as the Set element

>>s=set ([' A ', ' B ', ' C '])
>>print s
Set ([' A ', ' C ', ' B '])
Note: It is possible to see that the order of printing is different from the original list order because the elements stored inside the set are unordered
>>s=set ([' A ', ' B ', ' C ', ' C '])
>>print s
Set ([' A ', ' C ', ' B '])
>>len (s)
3
The results show that because the set cannot contain duplicate elements, set automatically removes the duplicate elements, the original list has 4 elements, but set has only 3
Programming tasks: Please use set to indicate 4 students in the class: Adma,lisa,bart,paul
>>s=set ([' Adma ', ' Lisa ', ' Bart ', ' Paul ')
Print S
Operation Result:
Set ([' Lisa ', ' Paul ', ' Adma ', ' Bart '])

Access Set
Since set stores an unordered collection, we cannot access it through an index, and accessing an element in a set is actually determining whether an element is in set, which can be judged by the in operator.

>>s=set ([' Adma ', ' Lisa ', ' Bart ', ' Paul ')
>> ' Bart ' in S
True
>> ' Bill ' in S
False
>> ' Bart ' in S
False
Note: case (case sensitive)
Features of Set:
The internal structure of set is much like the dict, the only difference is that it does not store value, therefore, determining whether an element is fast in set, the set stored element and the Dict key are similar, must be immutable objects, therefore, any mutable object cannot be placed in the set. Finally, the set stores the elements that are not in order.
For example: To determine if user input is a valid week?
Weekdays=set ([' Mon ', ' Tue ', ' Wed ', ' Thu ', ' Fri ', ' Sat ', ' Sun '])
x= '??? ' #用户输入的字符串
If X in weekdays:
print ' Input ok '
Else
print ' input error '

Traversing set
Because set is also a collection, traversing set is similar to traversing a list, and can be implemented through a for loop

>>s=set ([' Adma ', ' Lisa ', ' Bart ')
>>for name in S:
... Print Name
...
Lisa
Adma
Bart
Note: For loops that are traversing set, the order of the elements and the order of the list are likely to be different

Update set (add, remove)
Because set stores a set of unordered elements that are not duplicated, the update set mainly does two things:
。 is to add a new element to the set
。 is to remove an existing element from the set (provided that the element exists)



So with Add () can be added directly, and remove () before you need to judge

Python Learning notes (3)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.