Python Basics-Dictionaries

Source: Internet
Author: User

#########################################################################
# Dict
# Dict
# dic = {
# "K1": ' V1 ',
# "K2": ' V2 '
# }
# 1 Create a dictionary based on the sequence, and specify a uniform value
# v = Dict.fromkeys (["K1", 123, "999"],123)
# print (v)

# 2 You can specify the default value (None) If the key does not exist.
# v = dic[' k11111 ')
# print (v)
# v = dic.get (' K1 ', 111111)
# print (v)

# 3 Delete and get values
# dic = {
# "K1": ' V1 ',
# "K2": ' V2 '
# }
# v = dic.pop (' K1 ', 90)
# Print (DIC,V)
# k,v = Dic.popitem ()
# Print (DIC,K,V)

# 4 Set the value,
# already exists, does not set, gets the value corresponding to the current key
# does not exist, set, gets the value corresponding to the current key
# dic = {
# "K1": ' V1 ',
# "K2": ' V2 '
# }
# v = dic.setdefault (' k1111 ', ' 123 ')
# Print (DIC,V)

# 5 Update
# dic = {
# "K1": ' V1 ',
# "K2": ' V2 '
# }
# dic.update ({' K1 ': ' 111111 ', ' K3 ': 123})
# Print (DIC)
# dic.update (k1=123,k3=345,k5= "ASDF")
# Print (DIC)

# 6 keys ()

7 VALUES ()

8 items ()

9 Get

10update
##########################################################################

#1, basic institutions
# info = {
# "K1": "V1", # Key-value pairs
# "K2": "V2"
# }


The value of the #2 dictionary can be anything
# info = {
# "K1": 18,
# "K2": True,
# "K3": [
# 11,
# [],
# (),
# 22,
# 33,
# {
# ' KK1 ': ' Vv1 ',
# ' KK2 ': ' Vv2 ',
# ' KK3 ': (11,22),
# }
# ],
# "K4": (11,22,33,44)
# }
# Print (info)

# 3 Boolean (1,0), list, dictionary cannot be a key for a dictionary
# info ={
# 1: ' Asdf ',
# "K1": ' Asdf ',
# True: "123",
# # [11,22]: 123
# (11,22): 123,
# # {' K1 ': ' v1 '}: 123
#
# }
# Print (info)

#4 Dictionary unordered

# info = {
# "K1": 18,
# "K2": True,
# "K3": [
# 11,
# [],
# (),
# 22,
# 33,
# {
# ' KK1 ': ' Vv1 ',
# ' KK2 ': ' Vv2 ',
# ' KK3 ': (11,22),
# }
# ],
# "K4": (11,22,33,44)
# }
# Print (info)

# 5, index method to find the specified element
# info = {
# "K1": 18,
# 2:true,
# "K3": [
# 11,
# [],
# (),
# 22,
# 33,
# {
# ' KK1 ': ' Vv1 ',
# ' KK2 ': ' Vv2 ',
# ' KK3 ': (11,22),
# }
# ],
# "K4": (11,22,33,44)
# }
# # v = info[' K1 ')
# # print (v)
# # v = info[2]
# # print (v)
# v = info[' K3 '][5][' KK3 '][0]
# print (v)

# 6 Dictionary Support del Delete
# info = {
# "K1": 18,
# 2:true,
# "K3": [
# 11,
# [],
# (),
# 22,
# 33,
# {
# ' KK1 ': ' Vv1 ',
# ' KK2 ': ' Vv2 ',
# ' KK3 ': (11,22),
# }
# ],
# "K4": (11,22,33,44)
# }
# del info[' K1 ')
#
# del info[' K3 '][5][' kk1 ']
# Print (info)

# 7 For Loop
# Dict
# info = {
# "K1": 18,
# 2:true,
# "K3": [
# 11,
# [],
# (),
# 22,
# 33,
# {
# ' KK1 ': ' Vv1 ',
# ' KK2 ': ' Vv2 ',
# ' KK3 ': (11,22),
# }
# ],
# "K4": (11,22,33,44)
# }
# for item in info:
# Print (item)
#
# for item in Info.keys ():
# Print (item)

# for item in Info.values ():
# Print (item)

# for item in Info.keys ():
# print (Item,info[item])

# for K,v in Info.items ():
# Print (K,V)

# True 1 False 0
# info ={
# "K1": ' Asdf ',
# True: "123",
# # [11,22]: 123
# (11,22): 123,
# # {' K1 ': ' v1 '}: 123
#
# }
# Print (info)

Python Basics-Dictionaries

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.