📜  映射抽象数据类型

📅  最后修改于: 2022-05-13 01:57:53.570000             🧑  作者: Mango

映射抽象数据类型

ADT 代表抽象数据类型。此数据类型由程序员定义。抽象数据类型包含一组定义良好的值和操作。 ADT 在编程中充当资产,因为它的实现可以在不修改程序的其余部分的情况下进行更改。借助抽象数据类型,我们还可以减少逻辑错误。由于现在您已经掌握了抽象数据类型的要点,现在让我们继续讨论我们的主题,即 Map 抽象数据类型。

映射抽象数据类型

映射是包含记录集合的抽象数据类型。它是一个接口,说明可以执行的所有操作,但不说明它们的实现。地图的每条记录都包含一个键和一个值。映射的属性使得每个值都可以在键的帮助下访问或检索。这意味着每个键都与一个值相关联。需要注意的是,映射中的每个键都必须是唯一的,这一点非常重要。值可以重复,但键每次都必须是唯一的。在各种编程语言中,地图的实现有不同的名称。看下面的例子:

  1. C++:unordered_map、multimap、hash_map等。是实现。
  2. Python:字典。
  3. Java:哈希映射、哈希表。

地图的操作

现在让我们熟悉一些可以使用地图执行的有用操作:

  • put(key, value):将新的键值对添加到映射中。如果键已经存在于映射中,则旧值将被新值替换。
  • get(key):这里传递了一个键,在键的帮助下,我们可以检索与给定键关联的值。
  • len():此操作返回映射的长度,即键值对的数量。
  • del:此操作用于从映射中删除特定的键值对。这就是它的完成方式:del map[key]。特定键及其关联值将从地图中删除。
  • in:此操作返回一个布尔值。如果给定的键存在于地图中,则返回 true,否则返回 false。

地图的用途

在以下情况下,地图抽象数据类型非常适合使用:

  • 一个学生的分数由他/她的学生证。
  • 按社会保险号分类的税务数据。
  • 员工的工资按员工 ID 计算。
  • 拥有车主牌照的摩托车车主。

地图也高度用于计算频率。例如:计算数组中的重复数字。这里可以使用映射,其中键是数字的索引,值是它的频率。下表描述了键值对的示例:

Roll No.Name
101Shawn
102John
103David
104John

在上表中,Roll No. 是键,Name 是值。所以你可以看到值'John'已经出现了两次,但是由于它们的键,即卷号不同,它们可以被地图存储和识别。所以从上面的例子中,很明显我们只需要key就可以访问或删除任何值。关于映射的一个重要事实是,不必具有相同数据类型的键和值。该映射允许我们分别为键和值使用不同的数据类型。

地图的实现

Map 也称为关联数组。所以基本上,为了实现地图,我们需要两个数据值。第一个是键,您可以借助它访问、删除、替换与其关联的值。值是创建地图时采用的第二个参数。我们可以使用以下两种方式来实现地图:

  • 列表
  • 字典

让我们一一讨论。

列表:

现在,有不同的方式可以实现地图,但目前,列表似乎是最好的。列表用于表示顺序非常重要的值的集合或集合。因此,要使用列表实现地图,我们必须并行创建两个列表。其中第一个列表用于存储键,第二个列表用于存储值。查看以下示例,其中创建了 2 个列表。

清单 1:

IndexPin code
0400099
1500078
2340005

清单 2:

IndexRoad name
0Diagon Alley
1Hagrid Rd.
2Albus Warne 

以上两个列表是使用对偶列表实现映射的示例。第一个列表包含作为 PIN 码的键,第二个列表包含作为与特定键关联的值的道路名称。

例子:

Python
# Python program to implement map using dual list
  
# keys
listA = ['one', 'two', 'three']  
  
# values
listB = ['Cricket', 'Football', 'Wrestling']  
  
# map function creates a new array for every 
# element of a calling function.
print(list(map(lambda x, y: x + ' ' + y, listA, listB)))


Python
# Python program to implement map using dictionary
  
# Creating a dictionary
Sports_dict = {
    "Name": "Virat",
    "Sport": "Cricket",
    "Runs": 12000
}
  
# Prints the entire dictionary in key-value pair
print(Sports_dict)
  
# Prints the value associated with the following key
print(Sports_dict["Runs"])


Python3
# Python3 program to create a list of the number of 
# goals scored by every player in a team of 6 with 
# their names using a map
  
# Implementing the map using dictionary
Super_FC = {'Chris': 7, 'Mark': 3, 'Paul': 6, 
            'Adam': 8, 'Micheal': 10, 'Stuart': 12}
  
# Prints the dictionary
print(Super_FC)


Python3
# Python3 program to create a list of car and 
# their colors. Find out the color of a specific car.
  
# Implementing the map using dictionary
cars = { 'Jaguar': 'Black Color', 'Ferrari': 'Red Color',
         'Mercedes': 'White Color'}
  
# Prints dictionary
print(cars) 
  
# Print the color of Ferari
print(cars['Ferrari'])


Python3
# Python3 program to remove a particular sentence 
# from a specific no. of the line from a book.
  
# Creating book dictionary
book = { 'line1': 'Hello World!', 'line2': 'I am the best',
         'line3': 'The Giant is here!!', 'line4': 'Yes!',
         'line5': 'Big Show'}
print("Before deleting : ")
print(book)
  
# line1 deleted
del book['line1']  
print("After deleting line1 : ")
print(book)


输出
['one Cricket', 'two Football', 'three Wrestling']

字典

另一种实现地图的方法是使用内置的字典数据类型。字典帮助我们创建一个列表,其中包含类似于地图的键和值。在Python中,可以通过在花括号 {} 中插入一组元素来创建字典,花括号之间用“逗号”分隔。重要的是要注意值可以更改,但键是不可变的。字典的重要特征是它不允许重复。字典的另一个属性是它在插入时本质上是有序的,这意味着项目具有无法更改的已定义顺序。尽管字典是可变的,这意味着即使在创建字典之后我们也可以添加、删除或替换元素。在了解如何使用字典来实现地图之前,让我们先了解一些重要的字典方法。

MethodDescription
get()This method is used to return the value of the specified key.
clear()This method is used to remove all the elements from the dictionary.
pop()This method is used to remove the element associated with the specified key.
update()This method is used to update the dictionary with the specified key-value pairs.
values()This method is used to return a list of all the values present in the dictionary.
keysThis method is used to return a list of all the keys present in the dictionary.

例子:

Python

# Python program to implement map using dictionary
  
# Creating a dictionary
Sports_dict = {
    "Name": "Virat",
    "Sport": "Cricket",
    "Runs": 12000
}
  
# Prints the entire dictionary in key-value pair
print(Sports_dict)
  
# Prints the value associated with the following key
print(Sports_dict["Runs"])
输出
{'Runs': 12000, 'Sport': 'Cricket', 'Name': 'Virat'}
12000

示例问题

问题 1:使用地图创建一个 6 人团队中每个球员的进球数列表,并用他们的名字命名。

解决方案:

Python3

# Python3 program to create a list of the number of 
# goals scored by every player in a team of 6 with 
# their names using a map
  
# Implementing the map using dictionary
Super_FC = {'Chris': 7, 'Mark': 3, 'Paul': 6, 
            'Adam': 8, 'Micheal': 10, 'Stuart': 12}
  
# Prints the dictionary
print(Super_FC)  
输出
{'Chris': 7, 'Mark': 3, 'Paul': 6, 'Adam': 8, 'Micheal': 10, 'Stuart': 12}

问题 2:创建汽车及其颜色的列表。找出特定汽车的颜色。

解决方案:

Python3

# Python3 program to create a list of car and 
# their colors. Find out the color of a specific car.
  
# Implementing the map using dictionary
cars = { 'Jaguar': 'Black Color', 'Ferrari': 'Red Color',
         'Mercedes': 'White Color'}
  
# Prints dictionary
print(cars) 
  
# Print the color of Ferari
print(cars['Ferrari'])  
输出
{'Jaguar': 'Black Color', 'Ferrari': 'Red Color', 'Mercedes': 'White Color'}
Red Color

问题 3:从书中特定行数中删除特定句子。

解决方案:

Python3

# Python3 program to remove a particular sentence 
# from a specific no. of the line from a book.
  
# Creating book dictionary
book = { 'line1': 'Hello World!', 'line2': 'I am the best',
         'line3': 'The Giant is here!!', 'line4': 'Yes!',
         'line5': 'Big Show'}
print("Before deleting : ")
print(book)
  
# line1 deleted
del book['line1']  
print("After deleting line1 : ")
print(book)

输出

Before deleting : 
{'line1': 'Hello World!', 'line2': 'I am the best', 
 'line3': 'The Giant is here!!', 'line4': 'Yes!', 
 'line5': 'Big Show'}
After deleting line1 : 
{'line2': 'I am the best', 'line3': 'The Giant is here!!', 
 'line4': 'Yes!', 'line5': 'Big Show'}