python basics - dictionary

################################################## #######################
# 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 Get the value according to the Key. When the key does not exist, you can specify the default value (None)
  # v = dic['k11111']
  # print(v)
  # v = dic.get('k1',111111)
  # print(v )

# 3 delete and get the value
  # 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, do not set, get the value corresponding to the current key
  # does not exist, set, get the value corresponding to the current key
  # dic = {
  # "k1": 'v1',
  # "k2": 'v2 '
  # }
  # v = dic.setdefault('k1111','123')
  # print(dic,v)

# 5 更新
  # 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 organization
# info = {
# "k1": "v1", # key-value pair
# "k2": "v2"
# }


#2 The value of the dictionary can be any value
# 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 values ​​(1,0), lists, and dictionaries cannot be used as dictionary keys
# 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. Find the specified element by index
# 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 supports 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循环
# 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 and 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)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325080692&siteId=291194637