0 Basic Learning Python_ dictionary (25-26 lessons)

Source: Internet
Author: User
Tags shallow copy

Today learned the knowledge behind, suddenly found that before the list of the dictionary to learn a bit of forgetting, and intend to complement the previous learning of the dictionary, when you look at the time, the string, list, dictionary, ganso these few orders, just start learning the next method, of course, you can memorize, The back of the use of the time do not remember to look at it, practice makes perfect! Today is mainly about the method of the dictionary.

In relatively formal terms, the dictionary in Python is a data structure of Python, which is essentially a collection of key and value and its corresponding relationship, and a key can correspond to more than one value. In fact, you can easily understand the example.

1, the creation of the dictionary (everything must be born first)

There are several ways to create a dictionary:

The first, separated by commas directly in the middle of the two curly braces

The second is to assign values directly by Dict this object

Third, convert Ganso and lists to dictionaries by Dict objects

Very magical, actually there is a way to create, but also to everyone say it

The fourth type, Dict1.fromkeys (Seq[,val]), creates and returns a new dictionary, with the element in SEQ making the dictionary key, and Val does the initial value corresponding to all keys in the dictionary (default is None);

Show us the following:

From here it can be seen that if you do not set Val then the default is None, we also do a knock, hehe!

Then since the creation of a good, then you can do the most classic increase, delete, check, change the operation of, haha ha!

In order to let everyone have an impression I first put the dictionary commonly used methods to show you:

dict1.clear ()                              #移除dict1中的所有项  dict1.copy ()                               #返回dict1的副本  Dict1.fromkeys (Seq[,val])                  # Creates and returns a new dictionary that is the key to the dictionary with the elements in the SEQ, and Val does the initial value corresponding to all keys in the dictionary (default is None); Dict1. Get (Key[,default])                   #如果dict1 [key] exists, returns it, or returns the given default value of None  
Dict1.keys ()                               #返回dict1键的列表  dict1.pop (Key[,hello])                     #[] can be no, Dict1.pop (Key[,default]) and get methods similar. If there is a key in the dictionary, delete and return the key corresponding to the Vuale; if the key does not exist and the default value is not given, the keyerror exception is thrown;
Dict1.popitem () #从dict1中移除任意一项 and takes it as (key, value) to return Dict1.setdefault (Key[,default]) #如果dict1 [key] Values that exist then return it to key, otherwise the default value of None dict1.update (Other) #将other中的每一项加入到dict1中 is returned . dict1.values () #返回dict1中值的列表
Dict1.copy () #返回一个字典的浅复制
Dict1.items ()                              #返回表示dict1项的 (key, value)                          returns an iterative object  in the (key, value) pair returned by the list Dict1.iteritems () #从dict1. Items (). Dict1.iterkeys ()                           #从dict1的键中返回一个可迭代对象  dict1.itervalues ()                         #从dict1的值中返回一个可迭代对象  

2. Dictionary Increase

This is relatively simple, the following dictionary method is the default has created a dictionary, the content is Dict1 = {' x ': 1, ' Y ': 2}

To add content, simply write the new key to add it, as shown on the right

3. Dictionary deletion

There are several ways to delete the words:

1.dict1.pop (Key[,default]) is similar to the Get method. If there is a key in the dictionary, delete and return the key corresponding to the Vuale; if the key does not exist and the default value is not given, the keyerror exception is thrown;

Specify key deletion via pop and return hello default if key does not exist

2.dict1.popitem (), this is a random deletion of the dictionary, you may feel that you use to delete the last item, but because the dictionary itself is unordered, so there is no saying must delete a value, high efficiency

3.dict1.clear (), the clear method clears all items in the dictionary. This is an in-place operation, so no return value (or none) is returned.

4.del Dict1 or del Dict1[key], delete the dictionary value or dictionary, the dictionary will not exist after the dictionary is deleted

4. Dictionary modification

There are several ways to modify the dictionary, ah, the method is really many, is not remember very egg pain ah, ha ha, not actually, with a hand knock, with the attitude of interest to see, I believe you will become very fond of.

The most convenient way to modify a dictionary is to assign a value directly, for example

Is it particularly simple? Directly through key modification can be

Here's another way to say a dictionary update: Dict1.update (Other), what is this method? is to add a dictionary to another dictionary, without adding it, and modifying it.

5. Dictionary Search

We create a dictionary is often to save the data, since we have saved the data we want, it is necessary to take out at the appropriate time, this is the dictionary is more important, the method is more, in fact, we learn the database, Linux for the search command is very important and more, the following to everyone to introduce the next bar !

1.dict1.get (Key[,default]), if Dict1[key] exists, returns it, otherwise returns the given default value of None, see the example below and you'll know.

2.in, similar to python2.7 inside the has_key, Determine whether a key is in a dictionary, there is true no false

3. Query value directly from the key value,

4.dict1.keys (), the keys method returns the keys in the dictionary as Dict_keys, Dict1.values (), and the values method returns the keys in the dictionary in dict_values form

.

5.dict1.setdefault (Key,default=none), similar to get (), but if the key does not exist in the dictionary, the key is added and the value is set to default

6.dict1.items (), returns the array of traversed (key, value) tuples as a list

Of course, in addition to the above there is a copy of the method, this is also here to say, although not what query related, this is a copy, also known as a shallow copy.

Dict1.copy (), what is a shallow copy look at the following example to know

The dict2 in the instance is actually a reference (alias) of Dict1, so the output is consistent. Dict3 a deep copy of the parent object (that is, a deep copy of the first-level directory) is not modified with dict1 modification, and the sub-object (Level two directory) is a shallow copy and is modified with dict1 modification.

0 Basic Learning Python_ dictionary (25-26 lessons)

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.