Python BASICS (5)-dictionary

Source: Internet
Author: User

A dictionary consists of multiple keys and their corresponding values (the key-value pair is an item). Each key and its value are separated by a colon, items are separated by commas (,), and the entire dictionary is enclosed by a pair of braces. The empty dictionary consists of two braces :{}

Dict Functions

You can use dict functions to create a dictionary through other mappings or sequences such as keys and values.

>>> items=[(,),(,42>>> d=>>>: 42, : >>> d[

Dict functions can also create dictionaries Using Keyword parameters:

>>> d=dict(name=,age=42>>>: 42, : }

Basic dictionary operations:

  • The basic behavior of the dictionary is similar to that of the sequence in many aspects:
  • Len (d) returns the number of key-value pairs in d.
  • D [k] returns the value associated with the key k.
  • D [k] = v associate the value v with the key k
  • Del d [k] delete key k
  • K in d check whether d contains k-key items

Note:

Key type: the keys in the dictionary are not necessarily set to integer data, or they may be of other immutable types.

Auto-add: even if the key does not exist in the dictionary at the beginning, you can assign a value to it so that the dictionary creates a new item, the value cannot be associated with an index outside the list range.

Membership: expression k in d (d is a dictionary) searches for keys rather than values. Expression v in l (l is a list) is used to find the value, rather than Index

Dictionary Method:

1. clear

The clear method clears all the items in the dictionary. This is an in-situ operation, so no return value is returned.

>>> d=>>> d[]=>>> d[]=42>>>: 42, : >>> return_value=>>>>>> 

Consider the following two situations:

>>> x=>>> y=>>> x[]=>>>: >>> x=>>>: >>> >>> x=>>> y=>>> x[]=>>>: >>>>>>

2. copy

The copy method returns a new dictionary with the same key-value pairs (this method implements shortest copy)

>>> x={:,:[,,>>> y=>>> y[]=>>> y[].remove(>>>: , : [, >>>: , : [, ]}

From the code above, we can see that the original dictionary is not affected when the value is replaced in the copy. However, if a value is modified, the original dictionary will also change.

One way to avoid this problem is to use deep replication to copy all the values it contains. You can use the deepcopy function of the copy module to complete the operation:

>>>  copy >>> d=>>> d[]=[,>>> c=>>> dc=>>> d[].append(>>>: [, , >>>: [, ]}

3. fromkeys

The fromkeys method creates a new dictionary using the given key. The default value of each key is None.

>>> {}.fromkeys([,: None, : None}

4. get

The get method is a more loose Method for accessing dictionary items. Generally, an error occurs when you try to access an item that does not exist in the dictionary:

>>> d=>>>  d[, line 1,  <module>     d[>>>  d.get()

You cannot use get.

::::::::::::= Raw_input (request = raw_input (key = request =: key = person. get (key, % >>>================================== RESTART ==== ==========================================>>>Simple database example using get

5. has_key

The has_key method can check whether the dictionary contains the given key. The expression d. has_key (k) is equivalent to the expression k in d.

>>> d=>>> d.has_key(>>> d[]=>>> d.has_key(

6. items and iteritems

The items method returns all dictionary items in the list. Each item in the list comes from (Key, value), but there is no special order for the items to be returned.

>>> d={:,:,>>>, ), (, 0), (, )]

The iteritems method works roughly the same but returns an iterator object instead of a list:

>>> it=>>><dictionary-itemiterator object at 0x0280F6F0>>>>, ), (, 0), (, )]

7. pop

The pop method is used to obtain the value corresponding to the given key, and then remove the key-value pair from the dictionary.

>>> d={:1,:2>>> d.pop(1>>>: 2}

8. popitem

The popitem method is similar to list. pop. The latter will pop up the last element of the list. However, popitem pops up random items.

>>> d={:,:,>>>: , : 0, : >>>, >>>: 0, : }

9. setdefault

The setdefault method is similar to the get method to some extent. In addition, setdefault can also set the corresponding key value without a given key in the dictionary.

>>> d=>>> d.setdefault(,>>>: >>> d[]=>>> d.setdefault(,>>>: }

10. update

The update method can use one dictionary item to update another dictionary:

>>> d=:::>>> x={:>>>>>>: , : , : }

11. values and itervalues

The values method returns the value in the dictionary in the form of a list (itervalues return value iterator). Unlike the return key list, the returned value list can contain repeated elements:

>>> d=>>> d[1]=1>>> d[2]=2>>> d[3]=3>>> d[4]=1>>>1, 2, 3, 1]

 

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.