python字典{:>4}_升级您的Python技能:检查字典

python字典{:>4}

by Adam Goldschmidt

亚当·戈德施密特(Adam Goldschmidt)

升级您的Python技能:检查字典 (Upgrade your Python skills: Examining the Dictionary)

a hash table (hash map) is a data structure that implements an associative array abstract data type, a structure that can map keys to values.

哈希表(哈希表)是一种实现关联数组抽象数据类型的数据结构,该结构可以将键映射到值。

If it smells like a Python dict, feels like a dict, and looks like one… well, it must be a dict. Absolutely! Oh, And a set too...

如果它闻起来像Python dict ,感觉像是dict ,并且看起来像……,那么,那一定是dict 。 绝对! 哦还有set

?? (Huh?)

Dictionaries and sets in Python are implemented using a hash table. It may sound daunting at first, but as we investigate further everything should be clear.

Python中的字典和集合是使用哈希表实现的。 起初听起来可能令人生畏,但随着我们进一步调查,一切都应该清楚。

目的 (Objective)

Throughout this article, we will discover how a dict is implemented in Python, and we will build our own implementation of (a simple) one. The article is divided into three parts, and building our custom dictionary takes place in the first two:

在整个本文中,我们将发现如何在Python中实现dict ,并且将构建自己的实现(一个简单的实现)。 本文分为三个部分,构建我们的自定义词典发生在前两个部分:

  1. Understanding what hash tables are and how to use them

    了解什么是哈希表以及如何使用它们
  2. Diving into Python’s source code to better understand how dictionaries are implemented

    深入研究Python的源代码以更好地理解字典的实现方式
  3. Exploring differences between the dictionary and other data structures such as lists and sets

    探索字典与其他数据结构(例如列表和集合)之间的差异

什么是哈希表? (What is a hash table?)

A hash table is a structure that is designed to store a list of key-value pairs, without compromising on speed and efficiency of manipulating and searching the structure.

哈希表是一种旨在存储键-值对列表的结构,而不会影响操纵和搜索该结构的速度和效率。

The effectiveness of the hash table is derived from the hash function — a function that computes the index of the key-value pair — Meaning we can quickly insert, search and remove elements since we know their index in the memory array.

哈希表的有效性源自哈希函数 (该函数计算键值对的索引),这意味着我们可以快速插入,搜索和删除元素,因为我们知道它们在内存数组中的索引。

The complexity begins when two of our keys hash to the same value. This scenario is called a hash collision. There are many different ways of handling a collision, but we will only cover Python’s way. We won’t go too deep with our hash table explanation for the sake of keeping this article beginner-friendly and Python-focused.

当我们的两个键散列到相同的值时,复杂性就开始了。 这种情况称为哈希冲突 。 处理冲突有很多不同的方法,但是我们仅介绍Python的方法。 为了使本文对初学者友好且以Python为重点,我们对哈希表的解释不会太深。

Let’s make sure we wrapped our head around the concept of hash tables before moving on. We will start by creating the skeletons for our very (very) simple custom dict consisting of only insertion and search methods, using some of Python's

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值