Basic composition and usage of the Python dictionary

Source: Internet
Author: User
Tags print format

Basic composition and usage of the Python dictionary

#! /Usr/bin/env python
#-*-Coding: UTF-8 -*-
"""
Old Rules: Environment 2.7.x in the following method. Please remember the print format (the output content should be placed in parentheses) for users of version 3.x or later)
Basic composition and usage of dictionaries
Dict = {key: value}
Dict [key] = value
First, the dictionary is a basic structure composed of key keys and value values.
The key cannot be named by multiple elements such as list and dict dictionary,
The key is a unique attribute and can be called a one-to-one service. The key is the same but only one
The value can be named by one or more elements. It is not a unique attribute and can be called a one-to-many service.
The most important thing is that the dictionary is unordered.
Let's take a look at the usage of the dictionary:
"""
# Dic ={} initializes a dictionary
Dic_samekey = {"a": None, "a": None, "B": None, "B": None} # the same key
Print dic_samekey
Dic_morevalue = {"a": ["0", "1", "2"], "B": {"c": 0, "d": 1, "e": 2}, "t" :( 0, 1, 2)} # One-to-multiple value FEATURES
Print dic_morevalue
# It is a headache to see multiple values at this time,
# In fact, the value I named has been marked with a subscript and is also sorted by default, and the dictionary is unordered again
# Dict [key] = value in this way, you can name a dictionary and get a desired value.
Print dic_morevalue ["a"] [0], dic_morevalue ["a"] [1], dic_morevalue ["a"] [2]
Print dic_morevalue ["B"] ["c"], dic_morevalue ["B"] ["d"], dic_morevalue ["B"] ["e"] # multi-level selection can be constructed for multi-dictionary usage
Print dic_morevalue ["t"] [0], dic_morevalue ["t"] [1], dic_morevalue ["t"] [2]
# Quickly retrieve all the key methods and types of the dictionary
Print dic_morevalue.keys (), type (dic_morevalue.keys () # The returned type is list.
# Quickly obtain all value methods and types in the dictionary
Print dic_morevalue.values (), type (dic_morevalue.values () # is also a list
# Copy, as the name implies, is a copy (Shortest copy) commonly known as a value assignment.
Dic_test = dic_morevalue.copy ()
Dic = dic_test
Print dic_test
# Clear indicates clearing all elements in the dictionary.
Print dic_morevalue.clear ()
# Has_key is used to determine whether the key exists in the dictionary. the return value is Boolean, that is, True or False, True, and False.
Print dic_test.has_key ("B ")
# Get can also be used to determine whether the key exists in the dictionary. If no key exists, its default value None is returned.
Print dic_test.get ("k ")
# Pop is used to remove a dictionary key and its value.
B = dic_test.pop ("B ")
Print dic_test, u "removed B:", B
# The item () method combines every pair of key and value in the dictionary into a tuple, and puts these tuples in the list for return.
Item = dic_test.items ()
Print item
# Update: merge two dictionaries into the dictionary where update is used.
Dic2 = {"j": "nice "}
Dic_test.update (dic2)
Print dic_test
# Fromkeys name the value uniformly from the keys key queue. If it is not set, it is None.
Seq = ["name", "age", "job"]
Print dic_test.fromkeys (seq)
Print dic_test.fromkeys (seq, "guess") # name it guess

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.