使用字典的Python HashMap实现

In this tutorial you’ll learn how to implement hashmap in python using dictionary.

在本教程中,您将学习如何使用字典在python中实现哈希图。

What is Hash Table or Hash Map?

什么是哈希表或哈希图?

In short, Hash table is a data structure which stores data in an associative manner. In this table, our data is stored in a array format, where each data value have a unique key to represent the data. So in this way, access of data becomes very fast if we know the key of the desired data. So in Hash Table, insertion and search operations are very fast irrespective of the size of the data.

简而言之,哈希表是一种以关联方式存储数据的数据结构。 在此表中,我们的数据以数组格式存储,其中每个数据值都有一个表示数据的唯一键。 因此,如果我们知道所需数据的密钥,以这种方式,数据访问将变得非常快。 因此,在哈希表中,无论数据大小如何,插入和搜索操作都非常快。

How to Implement Hash Map in Python?

如何在Python中实现哈希映射?

Well if you’re looking for implementation of hash map in python then the answer is Dictionary. Python Dictionary is a built-in type that supports key-value pairs. Basically Dictionary is like a list but instead of an integer index it can be of any type. Another thing to note that a dictionary is un-ordered, it may not show the items in the order it was defined. So in this article we’ll learn about dictionaries and all the operations that we can perform on a dictionary.

好吧,如果您正在寻找在python中实现哈希映射的方法,那么答案就是字典。 Python词典是一种内置的类型,支持键值对。 基本上,字典就像一个列表,但它可以是任何类型,而不是整数索引。 还要注意的另一件事是,字典是无序的,它可能无法按定义的顺序显示项目。 因此,在本文中,我们将学习字典以及我们可以在字典上执行的所有操作。

使用字典的Python HashMap实现 (Python HashMap Implementation Using Dictionary)

创建字典 (Creating a Dictionary)

Let’s say we want to create a dictionary called phonebook where names will be used as keys and their phone numbers as values to the keys, then our code will be:

假设我们要创建一个名为phonebook的字典,其中名称将用作键,而其电话号码将用作键的值,那么我们的代码将是:

phonebook = {
	'name 1' : '9100000000',
	'name 2' : '9104043000',
	'name 3' : '9100004000',
}

Here phonebook  is the name of the dictionary and name 1, name 2 and name 3 are unique keys and each key represent to a phone number (data). Here the keys and data both are in string format but we can use any other datatype like int. 

这里的电话簿是字典的名称,名称1,名称2名称3是唯一键,每个键代表一个电话号码(数据)。 这里的键和数据都是字符串格式,但是我们可以使用其他任何数据类型,例如int。  

从字典访问数据 (Accessing Data from Dictionary)

We can print all the entries by using for loop or we can print a specific value using its key. Let’s say we want access the phone number of name 1.

我们可以使用for循环打印所有条目,也可以使用其键打印特定值。 假设我们要访问名称为1的电话号码。

print(phonebook['name 1'])

Output:

输出:

9100004000

9100004000

Or if you want to print all of the entries then the code will be.

或者,如果要打印所有条目,则代码为。

for k,v in phonebook.items():
	print(k + " - " + v)

Output:

输出:

name 3 – 9100004000 name 2 – 9104043000 name 1 – 9100000000 

名称3 – 9100004000 名称2 – 9104043000 名称1 – 9100000000  

Here k and v are loop variables, where k is used for key and v is for their values.

这里k和v是循环变量,其中k用于键,而v用于其值。

Note: Dictionary can’t have duplicate entry. If more than one keys (having same name) are present in the dictionary then the last entry will be stored.

注意:字典不能重复输入。 如果词典中存在多个(具有相同名称)的键,则将存储最后一个条目。

在字典中添加新条目或更新现有条目 (Add New Entry or Update Existing One Into Dictionary)

phonebook['name 2'] = '0581234323'	#updating existing entry
phonebook['name 4'] = '0681234323'	#creating a new entry
 
for k,v in phonebook.items():
	print(k+ " - " + v)

Output:

输出:

name 1 – 9100000000 name 4 – 0681234323 name 2 – 0581234323 name 3 – 9100004000

名称1 – 9100000000 名称4 – 0681234323 名称2 – 0581234323 名称3 – 9100004000

Here phonebook[key] = value can be used for both, to insert a new entry and also to update an existing entry. If the key is present in the dictionary then it will update the the value of that key with given value, if key doesn’t exist then It will add a new entry into the dictionary.

这里phonebook [key] = value可以用于插入新条目和更新现有条目。 如果密钥存在于字典中,则它将用给定值更新该密钥的值,如果密钥不存在,则它将在字典中添加一个新条目。

从字典中删除条目 (Deleting Entries from Dictionary)

To delete a specific entry from the dictionary:

要从字典中删除特定条目:

del phonebook['name 2']	#deleting a particular entry
 
for k,v in phonebook.items():
	print(k + " - " + v)

Output:

输出:

name 1 – 9100000000 name 3 – 9100004000

名称1 – 9100000000 名称3 – 9100004000

Here we’re deleting a particular entry from the dictionary phonebook using del statement.

在这里,我们使用del语句从字典电话簿中删除特定条目。

从字典中删除所有条目 (Delete All Entries from Dictionary)

phonebook.clear()	#deleting all the elements
 
print("phonebook  = " + str(phonebook))

Output:

输出:

phonebook  = {} 

电话簿= {}  

Here clear() method is used to clear all the entries in dictionary. The dictionary will be empty.

在这里, clear()方法用于清除字典中的所有条目。 字典将为空。

删除整个字典 (Delete Entire Dictionary)

del phonebook #deleting entire dictionary
print(phonebook)

Output:

输出:

NameError: name ‘phonebook’ is not defined

NameError:名称“电话簿”未定义

So del statement can be also used to delete the entire dictionary instead of a specific element.

因此, del语句也可以用于删除整个字典,而不是特定元素。

查找字典中的条目总数 (Find Total Number of Entries in Dictionary)

class_a = {1: 'Francis', 2: 'Mohan', 3: 'Steve'}
 
total_items = len(class_a) #Counting number of elements
 
print("No. of entries = " + str(total_items))

Output:

输出:

No. of entries = 3

条目数= 3

Here class_a is a dictionary which have students roll numbers as keys and name as values. Now to count the number of entries, we’re using len() function here. Which will count the number of elements in the dictionary class_a and will return the number to the variable total_items.

在这里, class_a是一本字典,其中学生卷号作为键,名称作为值。 现在要计算条目数,我们在这里使用len()函数。 它将计算字典class_a中的元素数量,并将该数量返回给变量total_items。

There are several other methods used by dictionaries like:

字典还使用其他几种方法,例如:

Let’s say our dictionary name is dict.

假设我们的字典名称是dict。

  • dict.copy() – returns a copy of dictionary dict.

    dict.copy()–返回字典dict的副本

  • dict.keys() – returns a list of keys present in dict.

    dict.keys()–返回字典中存在的键的列表

  • dict.values() – returns a list of values present in dict.

    dict.values()–返回字典中存在的值的列表

  • dict.items() – returns a list of tuple pairs (key values) present in dict.

    dict.items()–返回字典中存在的元组对(键值)的列表

  • cmp(dict1, dict2) – compare both of the dictionaries and returns true, false. The function cmp() can be used only in Python 2.

    cmp(dict1,dict2)–比较两个字典并返回true,false。 函数cmp()只能在Python 2中使用。

for more information on dictionaries in Python please visit https://docs.python.org/3/tutorial/datastructures.html

有关Python词典的更多信息,请访问https://docs.python.org/3/tutorial/datastructures.html

Comment below if you’ve any problem or suggestion related to python hashmap implementation using dictionary.

如果您对使用字典实现python hashmap有任何疑问或建议,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2018/06/python-hashmap.html

你可能感兴趣的:(列表,python,java,数据结构,数据分析)