This issue brings you a comprehensive analysis of the 11 methods of Python dictionary, I hope it will be helpful to you You helped.
dic = {key1 : value1, key2 : value2 }
Dictionaries are also called associative arrays or hash tables. Here are several common ways to create dictionaries:
# 方法1 dic1 = { 'Author' : 'Python当打之年' , 'age' : 99 , 'sex' : '男' } # 方法2 lst = [('Author', 'Python当打之年'), ('age', 99), ('sex', '男')] dic2 = dict(lst) # 方法3 dic3 = dict( Author = 'Python当打之年', age = 99, sex = '男') # 方法4 list1 = ['Author', 'age', 'sex'] list2 = ['Python当打之年', 99, '男'] dic4 = dict(zip(list1, list2))
print('methods = ',methods) methods = ['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
['clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
list1 = ['Author', 'age', 'sex'] list2 = ['Python当打之年', 99, '男'] dic1 = dict(zip(list1, list2)) # dic1 = {'Author': 'Python当打之年', 'age': 99, 'sex': '男'} dic1.clear() # dic1 = {}
list1 = ['Author', 'age', 'sex'] list2 = ['Python当打之年', 99, '男'] dic1 = dict(zip(list1, list2)) dic2 = dic1 # 浅拷贝: 引用对象 dic3 = dic1.copy() # 浅拷贝:深拷贝父对象(一级目录),子对象(二级目录)不拷贝,还是引用 dic1['age'] = 18 # dic1 = {'Author': 'Python当打之年', 'age': 18, 'sex': '男'} # dic2 = {'Author': 'Python当打之年', 'age': 18, 'sex': '男'} # dic3 = {'Author': 'Python当打之年', 'age': 99, 'sex': '男'}
import copy list1 = ['Author', 'age', 'sex'] list2 = ['Python当打之年', [18,99], '男'] dic1 = dict(zip(list1, list2)) dic2 = dic1 dic3 = dic1.copy() dic4 = copy.deepcopy(dic1) dic1['age'].remove(18) dic1['age'] = 20 # dic1 = {'Author': 'Python当打之年', 'age': 20, 'sex': '男'} # dic2 = {'Author': 'Python当打之年', 'age': 20, 'sex': '男'} # dic3 = {'Author': 'Python当打之年', 'age': [99], 'sex': '男'} # dic4 = {'Author': 'Python当打之年', 'age': [18, 99], 'sex': '男'}
list1 = ['Author', 'age', 'sex'] dic1 = dict.fromkeys(list1) dic2 = dict.fromkeys(list1, 'Python当打之年') # dic1 = {'Author': None, 'age': None, 'sex': None} # dic2 = {'Author': 'Python当打之年', 'age': 'Python当打之年', 'sex': 'Python当打之年'}
list1 = ['Author', 'age', 'sex'] list2 = ['Python当打之年', [18,99], '男'] dic1 = dict(zip(list1, list2)) Author = dic1.get('Author') # Author = Python当打之年 phone = dic1.get('phone') # phone = None phone = dic1.get('phone','12345678') # phone = 12345678
list1 = ['Author', 'age', 'sex'] list2 = ['Python当打之年', [18,99], '男'] dic1 = dict(zip(list1, list2)) items = dic1.items() print('items = ', items) print(type(items)) print('items = ', list(items)) # items = dict_items([('Author', 'Python当打之年'), ('age', [18, 99]), ('sex', '男')]) # <class 'dict_items'> # items = [('Author', 'Python当打之年'), ('age', [18, 99]), ('sex', '男')]
list1 = ['Author', 'age', 'sex'] list2 = ['Python当打之年', [18,99], '男'] dic1 = dict(zip(list1, list2)) keys = dic1.keys() print('keys = ', keys) print(type(keys)) print('keys = ', list(keys)) # keys = dict_keys(['Author', 'age', 'sex']) # <class 'dict_keys'> # keys = ['Author', 'age', 'sex']
list1 = ['Author', 'age', 'sex'] list2 = ['Python当打之年', [18,99], '男'] dic1 = dict(zip(list1, list2)) sex = dic1.pop('sex') print('sex = ', sex) print('dic1 = ',dic1) # sex = 男 # dic1 = {'Author': 'Python当打之年', 'age': [18, 99]}
list1 = ['Author', 'age', 'sex'] list2 = ['Python当打之年', [18,99], '男'] dic1 = dict(zip(list1, list2)) dic1.popitem() print('dic1 = ',dic1) # dic1 = {'Author': 'Python当打之年', 'age': [18, 99]}
list1 = ['Author', 'age', 'sex'] list2 = ['Python当打之年', [18,99], '男'] dic1 = dict(zip(list1, list2)) dic1.setdefault('Author', '当打之年') print('dic1 = ',dic1) # dic1 = {'Author': 'Python当打之年', 'age': [18, 99], 'sex': '男'} dic1.setdefault('name', '当打之年') print('dic1 = ',dic1) # dic1 = {'Author': 'Python当打之年', 'age': [18, 99], 'sex': '男', 'name': '当打之年'}
list1 = ['Author', 'age', 'sex'] list2 = ['Python当打之年', [18,99], '男'] dic1 = dict(zip(list1, list2)) print('dic1 = ',dic1) # dic1 = {'Author': 'Python当打之年', 'age': [18, 99], 'sex': '男'} list3 = ['Author', 'phone' ] list4 = ['当打之年', 12345678] dic2 = dict(zip(list3, list4)) print('dic2 = ',dic2) # dic2 = {'Author': '当打之年', 'phone': 12345678} dic1.update(dic2) print('dic1 = ',dic1) # dic1 = {'Author': '当打之年', 'age': [18, 99], 'sex': '男', 'phone': 12345678}
list1 = ['Author', 'age', 'sex'] list2 = ['Python当打之年', [18,99], '男'] dic1 = dict(zip(list1, list2)) values = dic1.values() print('values = ', values) print(type(values)) print('values = ', list(values)) # values = dict_values(['Python当打之年', [18, 99], '男']) # <class 'dict_values'> # values = ['Python当打之年', [18, 99], '男']
The above is the detailed content of Basics | Detailed explanations of 11 Python dictionary usages. For more information, please follow other related articles on the PHP Chinese website!

Article discusses impossibility of tuple comprehension in Python due to syntax ambiguity. Alternatives like using tuple() with generator expressions are suggested for creating tuples efficiently.(159 characters)

The article explains modules and packages in Python, their differences, and usage. Modules are single files, while packages are directories with an __init__.py file, organizing related modules hierarchically.

Article discusses docstrings in Python, their usage, and benefits. Main issue: importance of docstrings for code documentation and accessibility.

Article discusses lambda functions, their differences from regular functions, and their utility in programming scenarios. Not all languages support them.

Article discusses break, continue, and pass in Python, explaining their roles in controlling loop execution and program flow.

The article discusses the 'pass' statement in Python, a null operation used as a placeholder in code structures like functions and classes, allowing for future implementation without syntax errors.

Article discusses passing functions as arguments in Python, highlighting benefits like modularity and use cases such as sorting and decorators.

Article discusses / and // operators in Python: / for true division, // for floor division. Main issue is understanding their differences and use cases.Character count: 158


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!
