Python字典(避免错误)
Python中的dict是什么?
Python字典类似于 C++ 等语言中的哈希表。字典用于在Python中创建键值对。可以使用字符串编号和元组等代替键。代替值可以使用任何东西。 Python字典由大括号表示。空字典由 {} 表示。在Python字典中,键和值由 ':' 分隔,键值对由 ', ' 分隔。下面的例子清楚地解释了它
例子
Python3
# program to understand dictionary in python
# creating an empty dictionary
mydictionary ={}
# inserting values in dictionary
mydictionary ={'name':'Ankit',
'college':'MNNIT',
'address':'Allahabad'};
# print the dictionary
print(mydictionary)
Python3
# program to understand dictionary in python
# creating an empty dictionary
mydictionary ={}
# inserting values in dictionary
mydictionary ={'greeting':'Hello',
'status':'how are you',
'thanks':'thanks visit again'};
# print values according to choice
print(mydictionary['greeting'])
print(mydictionary['status'])
print(mydictionary['thanks'])
Python3
# program to understand dictionary in python
# creating an empty dictionary
mydictionary ={}
# inserting values in dictionary
mydictionary ={'greeting':'Hello',
'status':'how are you',
'thanks':'thanks visit again'};
# print values according to choice
print(mydictionary['greeting'])
print(mydictionary['status'])
print(mydictionary['thanks'])
# this will print none
print(mydictionary.get('college'))
# this will throw an error
print(mydictionary['college'])
Python3
# program to understand dictionary in python
# creating an empty dictionary
mydictionary ={}
# inserting values in dictionary
mydictionary ={'greeting':'Hello',
'status':'how are you',
'thanks':'thanks visit again'};
# print values according to choice
print(mydictionary['greeting'])
print(mydictionary['status'])
print(mydictionary['thanks'])
# copying dictionary
m = mydictionary
print(m)
输出 :
{'address': 'Allahabad', 'name': 'Ankit', 'college': 'MNNIT'}
在哪里使用?
字典使我们能够模拟各种现实世界的应用程序。我们可以创建一个人的字典,我们可以在其中存储与该人相关的所有信息,例如姓名年龄联系位置等。字典中可以存储任何类型的信息,例如单词及其含义。 Python字典在机器学习中使用得非常多,在这种情况下,机器与人交谈时,一些预定义的单词被存储为键,它们的含义作为值,当用户想要任何东西时,如果找到,则在键中搜索该东西,否则返回其值它显示了一些错误。事实上, Python字典可以在任何以普通旧语言使用散列的地方使用。
理解 dict 使用的示例
Python3
# program to understand dictionary in python
# creating an empty dictionary
mydictionary ={}
# inserting values in dictionary
mydictionary ={'greeting':'Hello',
'status':'how are you',
'thanks':'thanks visit again'};
# print values according to choice
print(mydictionary['greeting'])
print(mydictionary['status'])
print(mydictionary['thanks'])
输出 :
Hello
how are you
thanks visit again
使用 dicts 和克服时的常见错误
以下是在Python中使用 dict 时的一些错误。
1)在Python中访问字典元素永远不要使用键名直接访问元素总是尝试使用.get方法。如果 key 不存在,则 .get 方法将打印 none 而 [key] 将终止整个程序。
Python3
# program to understand dictionary in python
# creating an empty dictionary
mydictionary ={}
# inserting values in dictionary
mydictionary ={'greeting':'Hello',
'status':'how are you',
'thanks':'thanks visit again'};
# print values according to choice
print(mydictionary['greeting'])
print(mydictionary['status'])
print(mydictionary['thanks'])
# this will print none
print(mydictionary.get('college'))
# this will throw an error
print(mydictionary['college'])
输出 :
Hello
how are you
thanks visit again
None
运行时错误
Traceback (most recent call last):
File "/home/ce65dd34285f0cb0781de2a068e658fa.py", line 14, in
print(mydictionary['college'])
KeyError: 'college'
2)当我们想将一个字典复制到另一个字典时,应该有正确的复制方法知识
- new_dictionary = old_dictionary :这一行意味着 old_dictionary 和 new_dictionary 将引用同一个对象,这意味着一个字典中的更改将反映到另一个字典。
- new_dictionary = dict(old_dictionary) 和 new_dictionary = old_dictionary.copy() :将旧字典复制到新字典意味着 old 中的更新不会反映 d 中的更新,但 e 中的值将通过使用 references 复制。这将执行浅拷贝
- new_dictionary = copy.deepcopy(old_dictionary) :这将生成一个深拷贝。
Python3
# program to understand dictionary in python
# creating an empty dictionary
mydictionary ={}
# inserting values in dictionary
mydictionary ={'greeting':'Hello',
'status':'how are you',
'thanks':'thanks visit again'};
# print values according to choice
print(mydictionary['greeting'])
print(mydictionary['status'])
print(mydictionary['thanks'])
# copying dictionary
m = mydictionary
print(m)
输出 :
Hello
how are you
thanks visit again
{'greeting': 'Hello', 'status': 'how are you', 'thanks': 'thanks visit again'}
什么时候不使用 dicts ?
Python dict 在许多情况下很有用,但在某些情况下必须避免使用它们。在Python中,永远不要认为只有字典是关联数组。在 dict 我们应该尝试存储相同类型的值。
- 如果我们想搜索值是否存在于字典中,或者在这种情况下不总是使用Python set,因为在Python set 中,无论元素是否存在,它都是一个带有布尔值的关联数组。
- 对于固定数量的属性,请始终在Python中使用 Class 或命名元组。
- 当您想要一个特定键时的预定义消息时,然后在Python中使用 collections.defaultdict