如何在Python 3中将列表用作字典的键?

WBOY
发布: 2023-08-28 20:57:02
转载
2776人浏览过

如何在python 3中将列表用作字典的键?

字典是Python编程语言中最强大的数据结构之一。这是一个由键值对组成的数据结构。它具有几个优点;例如,访问值的时间复杂度为O(1),它在内存上高效,易于更新、删除和迭代,并提供许多内置函数进行快速操作。

当直接使用列表时出现的问题

我们关注这个主题,因为当我们尝试将列表作为键时会出现问题。列表是Python中的可变数据类型。因此,我们可以在列表内部删除、更新和追加值。因此,如果我们从列表和列表项生成一个哈希函数,当列表的项发生变化时,我们将无法再找到哈希函数,因为哈希函数已经改变。

另一个潜在的问题是不同的列表可能具有相同的哈希值。如果两个列表的值之和相同,它们的哈希值也将相同。在这种情况下,如果将其中一个列表用作字典中的键,并且搜索具有相同哈希值的另一个列表,字典可能会给出错误的结果。

Convert The List To Tuple

一种将字典的键列表转换为元组并将其用作键的方法是使用这种间接方式。请注意,尽管值保持不变,但数据类型不再是列表

立即学习Python免费学习笔记(深入)”;

语法

<variable name> = tuple(<list to convert into tuple>)
登录后复制

Here the tuple takes one parameter, which is the list's name.

Example

In the following code, we first created an empty dictionary named my_dict. Next, we created our list named my_list. We used the tuple method to convert the list into a Tuple object. We now used the Tuple object as the key as a string "hello, world!" as the value.

my_dict = {}
my_list = [1, 2, 3]
my_tuple = tuple(my_list)
my_dict[my_tuple] = "Hello, World!"
print(my_dict)
登录后复制

Output

{(1, 2, 3): 'Hello, World!'}
登录后复制

将列表转换为字符串

另一种方法是将列表转换为字符串数据类型。字符串也是不可变的数据类型;因此,我们可以将其用作字典的键。

爱图表
爱图表

AI驱动的智能化图表创作平台

爱图表 99
查看详情 爱图表

语法

<variable name> = ''.join(str(e) for e in <name of the list>)
登录后复制

这里的语句从列表中获取各个元素,并将其组合成一个字符串。Join函数将字符串序列连接起来。

Example

In the following code, we first created a list named my_list. Next, we used the join method that creates a string of list elements. Since the string can be used as the dictionary key, we used this as te key of the dictionary my_list_str.

my_list = [1, 2, 3]
my_list_str = ''.join(str(e) for e in my_list)
my_dict = {my_list_str: 'value'}
print(my_dict)
print(my_dict[my_list_str])
登录后复制

Output

{'123': 'value'}
value
登录后复制

将列表转换为JSON

We can also use the JSON module and built-in functions to convert the list first to string using the dumps method and later use this as the dictionary's key.

语法

<name of variable< = json.dumps(<name of list<)
登录后复制

这里是JSON库的dumps方法,它将Python对象序列化为JSON格式的字符串。dumps方法接受字符串的名称作为参数

Example

在下面的代码中,我们首先导入了JSON库。接下来,我们创建了一个名为my_list的列表。我们使用dumps方法从列表创建了一个序列化对象。现在,我们将序列化对象作为字典键my_dict。

import json
my_list = [1, 2, 3]
my_key = json.dumps(my_list)
my_dict = {my_key: 'value'}
print(my_dict)
登录后复制

Output

{'[1, 2, 3]': 'value'}
登录后复制

Conclusion

In this article, we learned how to use lists as the key of a dictionary in Python 3. We understood the difficulties we faced if we directly tried to make the lists as the keys in the Python dictionary. Therefore we first need to convert the list data type to tuples, strings, etc., which are immutable data types. We also learned how to use the JSON module to use lists as the dictionary key.

以上就是如何在Python 3中将列表用作字典的键?的详细内容,更多请关注php中文网其它相关文章!

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号