广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python Hashmap/Dicti
  • 124
分享到

Python Hashmap/Dicti

PythonHashmapDicti 2023-01-31 03:01:17 124人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

python 来自:Http://www.dotnetperls.com/dictionary-Python Built-in Dictionary List Set Tuple 2D Array Bytes Class Cons

python

来自:Http://www.dotnetperls.com/dictionary-Python


Built-in Dictionary List Set Tuple 2D Array Bytes Class Console Convert Datetime Duplicates Error File Find If Lambda Len Lower Map Math Namedtuple None Random Re Slice Sort Split String Strip Sub Substring TypeWhile

Dictionary. A dictionary optimizes element lookups. It associates keys to values. Each key must have a value. Dictionaries are used in many programs.
With square brackets, we assign and access a value at a key. With get() we can specify a default result. Dictionaries are fast. We create, mutate and test them.
Get example. There are many ways to get values. We can use the "[" and "]" characters. We access a value directly this way. But this syntax causes a KeyError if the key is not found.

Instead:We can use the get() method with one or two arguments. This does not cause any annoying errors. It returns None.

Argument 1:The first argument to get() is the key you are testing. This argument is required.

Argument 2:The second, optional argument to get() is the default value. This is returned if the key is not found.

Based on: Python 3

Python program that gets values

plants = {}

# Add three key-value tuples to the dictionary.
plants["radish"] = 2
plants["squash"] = 4
plants["carrot"] = 7

# Get syntax 1.
print(plants["radish"])

# Get syntax 2.
print(plants.get("tuna"))
print(plants.get("tuna", "no tuna found"))

Output

2
None
no tuna found
Get, none. In Python "None" is a special value like null or nil. I like None. It is my friend. It means no value. Get() returns None if no value is found in a dictionary.None

Note:It is valid to assign a key to None. So get() can return None, but there is actually a None value in the dictionary.


Key error. Errors in programs are not there just to t
ORMent you. They indicate problems with a program and help it work better. A KeyError occurs on an invalid access.KeyError
Python program that causes KeyError

lookup = {"cat": 1, "dog": 2}

# The dictionary has no fish key!
print(lookup["fish"])

Output

Traceback (most recent call last):
  File "C:\programs\file.py", line 5, in <module>
    print(lookup["fish"])
KeyError: 'fish'
In-keyWord. A dictionary may (or may not) contain a specific key. Often we need to test for existence. One way to do so is with the in-keyword.In

True:This keyword returns 1 (meaning true) if the key exists as part of a key-value tuple in the dictionary.

False:If the key does not exist, the in-keyword returns 0, indicating false. This is helpful in if-statements.

Python program that uses in

animals = {}
animals["monkey"] = 1
animals["tuna"] = 2
animals["giraffe"] = 4

# Use in.
if "tuna" in animals:
    print("Has tuna")
else:
    print("No tuna")

# Use in on nonexistent key.
if "elephant" in animals:
    print("Has elephant")
else:
    print("No elephant")

Output

Has tuna
No elephant
Len built-in. This returns the number of key-value tuples in a dictionary. The data types of the keys and values do not matter. Len also works on lists and strings.

Caution:The length returned for a dictionary does not separately consider keys and values. Each pair adds one to the length.

Python program that uses len on dictionary

animals = {"parrot": 2, "fish": 6}

# Use len built-in on animals.
print("Length:", len(animals))

Output

Length: 2
Len notes. Let us review. Len() can be used on other data types, not just dictionaries. It acts upon a list, returning the number of elements within. It also handles tuples.Len
Keys, values. A dictionary contains keys. It contains values. And with the keys() and values() methods, we can store these elements in lists.

Next:A dictionary of three key-value pairs is created. This dictionary could be used to store hit counts on a WEBsite's pages.

Views:We introduce two variables, named keys and values. These are not lists—but we can convert them to lists.

Convert
Python program that uses keys

hits = {"home": 125, "sitemap": 27, "about": 43}
keys = hits.keys()
values = hits.values()

print("Keys:")
print(keys)
print(len(keys))

print("Values:")
print(values)
print(len(values))

Output

Keys:
dict_keys(['home', 'about', 'sitemap'])
3
Values:
dict_values([125, 43, 27])
3
Keys, values ordering. Elements returned by keys() and values() are not ordered. In the above output, the keys-view is not alphabetically sorted. Consider a sorted view (keep reading).
Sorted keys. In a dictionary keys are not sorted in any way. They are unordered. Their order reflects the internals of the hashing alGorithm's buckets.

But:Sometimes we need to sort keys. We invoke another method, sorted(), on the keys. This creates a sorted view.

Python program that sorts keys in dictionary

# Same as previous program.
hits = {"home": 124, "sitemap": 26, "about": 32}

# Sort the keys from the dictionary.
keys = sorted(hits.keys())

print(keys)

Output

['about', 'home', 'sitemap']
Items. With this method we receive a list of two-element tuples. Each tuple contains, as its first element, the key. Its second element is the value.

Tip:With tuples, we can address the first element with an index of 0. The second element has an index of 1.

Program:The code uses a for-loop on the items() list. It uses the print() method with two arguments.

Python that uses items method

rents = {"apartment": 1000, "house": 1300}

# Convert to list of tuples.
rentItems = rents.items()

# Loop and display tuple items.
for rentItem in rentItems:
    print("Place:", rentItem[0])
    print("Cost:", rentItem[1])
    print("")

Output

Place: house
Cost: 1300

Place: apartment
Cost: 1000
Items, assign. We cannot assign elements in the tuples. If you try to assign rentItem[0] or rentItem[1], you will get an error. This is the error message.
Python error:

TypeError: 'tuple' object does not support item assignment
Items, unpack. The items() list can be used in another for-loop syntax. We can unpack the two parts of each tuple in items() directly in the for-loop.

Here:In this example, we use the identifier "k" for the key, and "v" for the value.

Python that unpacks items

# Create a dictionary.
data = {"a": 1, "b": 2, "c": 3}

# Loop over items and unpack each item.
for k, v in data.items():
    # Display key and value.
    print(k, v)

Output

a 1
c 3
b 2
For-loop. A dictionary can be directly enumerated with a for-loop. This accesses only the keys in the dictionary. To get a value, we will need to look up the value.

Items:We can call the items() method to get a list of tuples. No extra hash lookups will be needed to access values.

Here:The plant variable, in the for-loop, is the key. The value is not available—we would need plants.get(plant) to access it.

Python that loops over dictionary

plants = {"radish": 2, "squash": 4, "carrot": 7}

# Loop over dictionary directly.
# ... This only accesses keys.
for plant in plants:
    print(plant)

Output

radish
carrot
squash
Del built-in. How can we remove data? We apply the del method to a dictionary entry. In this program, we initialize a dictionary with three key-value tuples.Del

Then:We remove the tuple with key "windows". When we display the dictionary, it now contains only two key-value pairs.

Python that uses del

systems = {"Mac": 1, "windows": 5, "linux": 1}

# Remove key-value at "windows" key.
del systems["windows"]

# Display dictionary.
print(systems)

Output

{'mac': 1, 'linux': 1}
Del, alternative. An alternative to using del on a dictionary is to change the key's value to a special value. This is a null object refactoring strategy.
Update. With this method we change one dictionary to have new values from a second dictionary. Update() also modifies existing values. Here we create two dictionaries.

Pets1, pets2:The pets2 dictionary has a different value for the dog key—it has the value "animal", not "canine".

Also:The pets2 dictionary contains a new key-value pair. In this pair the key is "parakeet" and the value is "bird".

Result:Existing values are replaced with new values that match. New values are added if no matches exist.

Python that uses update

# First dictionary.
pets1 = {"cat": "feline", "dog": "canine"}

# Second dictionary.
pets2 = {"dog": "animal", "parakeet": "bird"}

# Update first dictionary with second.
pets1.update(pets2)

# Display both dictionaries.
print(pets1)
print(pets2)

Output

{'parakeet': 'bird', 'dog': 'animal', 'cat': 'feline'}
{'dog': 'animal', 'parakeet': 'bird'}
Copy. This method performs a shallow copy of an entire dictionary. Every key-value tuple in the dictionary is copied. This is not just a new variable reference.

Here:We create a copy of the original dictionary. We then modify values within the copy. The original is not affected.

Python that uses copy

original = {"box": 1, "cat": 2, "apple": 5}

# Create copy of dictionary.
modified = original.copy()

# Change copy only.
modified["cat"] = 200
modified["apple"] = 9

# Original is still the same.
print(original)
print(modified)

Output

{'box': 1, 'apple': 5, 'cat': 2}
{'box': 1, 'apple': 9, 'cat': 200}
Fromkeys. This method receives a sequence of keys, such as a list. It creates a dictionary with each of those keys. We can specify a value as the second argument.

Values:If you specify the second argument to fromdict(), each key has that value in the newly-created dictionary.

Python that uses fromkeys

# A list of keys.
keys = ["bird", "plant", "fish"]

# Create dictionary from keys.
d = dict.fromkeys(keys, 5)

# Display.
print(d)

Output

{'plant': 5, 'bird': 5, 'fish': 5}
Dict. With this built-in function, we can construct a dictionary from a list of tuples. The tuples are pairs. They each have two elements, a key and a value.

Tip:This is a possible way to load a dictionary from disk. We can store (serialize) it as a list of pairs.

Python that uses dict built-in

# Create list of tuple pairs.
# ... These are key-value pairs.
pairs = [("cat", "meow"), ("dog", "bark"), ("bird", "chirp")]

# Convert list to dictionary.
lookup = dict(pairs)

# Test the dictionary.
print(lookup.get("dog"))
print(len(lookup))

Output

bark
3
Memoize. One classic optimization is called memoization. And this can be implemented easily with a dictionary. In memoization, a function (def) computes its result.Memoize

And:Once the computation is done, it stores its result in a cache. In the cache, the argument is the key. And the result is the value.


Memoization, continued. When a memoized function is called, it first checks this cache to see if it has been, with this argument, run before.

And:If it has, it returns its cached—memoized—return value. No further computations need be done.

Note:If a function is only called once with the argument, memoization has no benefit. And with many arguments, it usually works poorly.


Get performance. I compared a loop that uses get() with one that uses both the in-keyword and a second look up. Version 2, with the "in" operator, was faster.

Version 1:This version uses a second argument to get(). It tests that against the result and then proceeds if the value was found.

Version 2:This version uses "in" and then a lookup. Twice as many lookups occur. But fewer statements are executed.

Python that benchmarks get

import time

# Input dictionary.
systems = {"mac": 1, "windows": 5, "linux": 1}

# Time 1.
print(time.time())

# Get version.
i = 0
v = 0
x = 0
while i < 10000000:
    x = systems.get("windows", -1)
    if x != -1:
        v = x
    i += 1

# Time 2.
print(time.time())

# In version.
i = 0
v = 0
while i < 10000000:
    if "windows" in systems:
        v = systems["windows"]
    i += 1

# Time 3.
print(time.time())

Output

1345819697.257
1345819701.155 (get = 3.90 s)
1345819703.453 (in  = 2.30 s)
String key performance. In another test, I compared string keys. I found that long string keys take longer to look up than short ones. Shorter keys are faster.Dictionary String Key
Performance, loop. A dictionary can be looped over in different ways. In this benchmark we test two approaches. We access the key and value in each iteration.

Version 1:This version loops over the keys of the dictionary with a while-loop. It then does an extra lookup to get the value.

Version 2:This version instead uses a list of tuples containing the keys and values. It actually does not touch the original dictionary.

But:Version 2 has the same effect—we access the keys and values. The cost of calling items() initially is not counted here.

Python that benchmarks loops

import time

data = {"michael": 1, "james": 1, "mary": 2, "dale": 5}
items = data.items()

print(time.time())

# Version 1: get.
i = 0
while i < 10000000:
    v = 0
    for key in data:
        v = data[key]
    i += 1

print(time.time())

# Version 2: items.
i = 0
while i < 10000000:
    v = 0
    for tuple in items:
        v = tuple[1]
    i += 1

print(time.time())

Output

1345602749.41
1345602764.29 (version 1 = 14.88 s)
1345602777.68 (version 2 = 13.39 s)
Benchmark, loop results. We see above that looping over a list of tuples is faster than directly looping over a dictionary. This makes sense. With the list, no lookups are done.
Frequencies. A dictionary can be used to count frequencies. Here we introduce a string that has some repeated letters. We use get() on a dictionary to start at 0 for nonexistent values.

So:The first time a letter is found, its frequency is set to 0 + 1, then 1 + 1. Get() has a default return.

Python that counts letter frequencies

# The first three letters are repeated.
letters = "abcabcdefghi"

frequencies = {}
for c in letters:
    # If no key exists, get returns the value 0.
    # ... We then add one to increase the frequency.
    # ... So we start at 1 and progress to 2 and then 3.
    frequencies[c] = frequencies.get(c, 0) + 1

for f in frequencies.items():
    # Print the tuple pair.
    print(f)

Output

('a', 2)
('c', 2)
('b', 2)
('e', 1)
('d', 1)
('g', 1)
('f', 1)
('i', 1)
('h', 1)
A summary. A dictionary is usually implemented as a hash table. Here a special hashing algorithm translates a key (often a string) into an integer.
For a speedup, this integer is used to locate the data. This reduces search time. For programs with performance trouble, using a dictionary is often the initial path to optimization.

--结束END--

本文标题: Python Hashmap/Dicti

本文链接: https://www.lsjlt.com/news/187176.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python Hashmap/Dicti
    Python 来自:http://www.dotnetperls.com/dictionary-python Built-in Dictionary List Set Tuple 2D Array Bytes Class Cons...
    99+
    2023-01-31
    Python Hashmap Dicti
  • Python hashmap
      标签: pythonhashmap 2014-10-15 18:42 533人阅读 评论(0) 收藏 举报  分类:   Python(40)  版权声明:本文为博主原创文章,未经博主允许不得转载。 [...
    99+
    2023-01-31
    Python hashmap
  • python实现一个简易hashmap
    ...
    99+
    2023-01-31
    简易 python hashmap
  • python中HashMap的一个实现
    class LinearMap(object): def __init__(self): self.items = [] def add(self, k, v): self.items....
    99+
    2023-01-31
    python HashMap
  • HashMap详解
    一、HashMap集合简介 HashMap 基于哈希表的 Map 接口实现,是以 key-value 存储形式存在,即主要用来存放键值对。HashMap 的实现不是同步的,这意味着它不是线程安全的。...
    99+
    2023-09-22
    数据结构 java 链表
  • Java HashMap透析
    HashMap 是数组和链表组合组成的复杂结构,哈希值决定了键值在数组的位置,当哈希值相同时则以链表形式存储,当链表长度到达设定的阈值则会对其进行树化,这样做是为了保证数据安全和数据相关操作的效率HashMap 性能表现取决于哈希码的有效性...
    99+
    2021-08-10
    java教程 Java
  • 什么是HashMap
    什么是HashMap,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。HashMap是一个非常重要的集合,日常使用也非常的频繁,同...
    99+
    2022-10-19
  • Java集合-HashMap
    目录概述重要的参数put函数的实现get函数的实现hash函数的实现RESIZE的实现概述 ①以数组+链表+红黑树实现。主要用来处理具有键值对特征的数据。②当链表长度大于阈值(或者红...
    99+
    2022-11-12
  • Java 中HashMap 详解
    本篇重点: HashMap的存储结构 HashMap的put和get操作过程 HashMap的扩容 关于transient关键字 HashMap的存储结构 HashMap 总体是数组+链表的存储结构, 从JDK1.8开始,当数组的长度大...
    99+
    2023-09-03
    哈希算法 散列表 java
  • HashMap实例分析
    这篇“HashMap实例分析”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“HashMap实例分析”文章吧。场景扮演面试官: ...
    99+
    2023-06-27
  • 怎么理解HashMap
    这篇文章主要讲解了“怎么理解HashMap”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么理解HashMap”吧!1、HashMap添加一个键值对的过程是怎么样的这是网上找的一张流程图,可...
    99+
    2023-06-16
  • HashMap的扩容机制
    目录 一、HashMap的底层 二、HashMap的扩容机制原理 1、JDK1.7版本扩容 2、JDK1.8版本扩容 三、HashMap底层JDK1.7到JDK1.8的变化 一、HashMap的底层 底层:采用数组+链表(JDK1.7)...
    99+
    2023-09-04
    java 面试 数据结构 链表 容器
  • javascript中有没有hashmap
    这篇文章主要介绍javascript中有没有hashmap,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完! javascript中有hashmap,其实现h...
    99+
    2022-10-19
  • Java中HashMap是什么
    这篇文章主要介绍Java中HashMap是什么,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!一、HashMap的结构图示本文主要说的是jdk1.8版本中的实现。而1.8中HashMap是数组+链表+红黑树实现的,大概...
    99+
    2023-06-15
  • HashMap和Hashtable的区别
    相同点: HashMap和Hashtable都是java.util包下的类HashMap和Hashtable都实现了Map接口,存储方式都是key-value形式HashMap和Hashtable同时...
    99+
    2023-09-01
    java 哈希算法 开发语言
  • java中hashmap怎么使用
    HashMap是Java中常用的数据结构之一,它是一个无序的键值对集合,可以存储不同类型的键和值。以下是HashMap的基本用法:1...
    99+
    2023-09-14
    java
  • Java HashMap源码是什么
    本篇内容主要讲解“Java HashMap源码是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Java HashMap源码是什么”吧!签名(signature)public cla...
    99+
    2023-06-17
  • HashMap底层原理分析
    小编给大家分享一下HashMap底层原理分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!众所周知,HashMap是一个用于存储Key-Value键值对的集合,每一个键值对也叫做Entry。这些个键值对(Entry)分散存...
    99+
    2023-06-27
  • hashmap底层实现原理
    一、hashmap底层实现原理 HashMap是基于哈希表的Map接口的非同步实现。元素以键值对的形式存放,并且允许null键和null值,因为key值唯一(不能重复),因此,null键只有一个。另外,hashmap不保证元素存储的顺...
    99+
    2023-10-29
    底层 原理 hashmap
  • Java之HashMap案例详解
    概述 这篇文章,我们打算探索一下Java集合(Collections)框架中Map接口中HashMap的实现。Map虽然是Collctions框架的一部分,但是Map并没有实现Col...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作