如何在 Python 中为字典建立索引
使用 list()
类来索引字典,例如 list(my_dict)[0]
和 list(my_dict.values())[0]
。 list()
类将字典或字典的值转换为我们可以在特定索引处访问的列表。
my_dict = {
'site': 'jiyik.com',
'color': 'blue',
'number': 100
}
# ✅ 通过索引获取字典中的键
key = list(my_dict)[0]
print(key) # 👉️ site
# ✅ 通过索引获取字典中的值
value = list(my_dict.values())[0]
print(value) # 👉️ bobbyhadz.com
# --------------------------------------
# ✅ 获取字典中键的索引
index = None
if 'site' in my_dict:
index = list(my_dict).index('site')
print(index) # 👉️ 0
我们使用 list()
类来索引字典。
list()
类将字典转换为键列表。
my_dict = {
'site': 'jiyik.com',
'color': 'blue',
'number': 100
}
print(list(my_dict)) # 👉️ ['site', 'color', 'number']
print(list(my_dict.keys())) # 👉️ ['site', 'color', 'number']
我们也可以使用更明确的 dict.keys()
方法。
dict.keys
方法返回字典键的新视图。
最后一步是访问特定索引处的键列表。
my_dict = {
'site': 'jiyik.com',
'color': 'blue',
'number': 100
}
key = list(my_dict)[0]
print(key) # 👉️ site
Python
索引是从零开始的,因此列表中的第一项的索引为 0,最后一项的索引为 -1 或len(my_list) - 1
。
如果我们需要获取字典中特定索引处的值,请将调用 dict.values()
的结果传递给 list()
类。
my_dict = {
'site': 'jiyik.com',
'color': 'blue',
'number': 100
}
value = list(my_dict.values())[0]
print(value) # 👉️ jiyik.com
dict.values
方法返回字典值的新视图。
my_dict = {
'site': 'jiyik.com',
'color': 'blue',
'number': 100
}
# 👇️ dict_values(['jiyik.com', 'blue', 100])
print(my_dict.values())
dict.values()
方法返回一个不可下标的视图对象(不能在索引处访问),因此我们仍然必须将其转换为列表。
如果我们需要获取字典中特定键或值的索引,请使用 list.index()
方法。
my_dict = {
'site': 'jiyik.com',
'color': 'blue',
'number': 100
}
index = None
if 'site' in my_dict:
index = list(my_dict).index('site')
print(index) # 👉️ 0
list.index()
方法返回值等于提供的参数的第一项的索引。
如果列表中没有此类项目,该方法将引发 ValueError
。
我们使用 if 语句来检查键是否存在于字典中,因此 list.index()
方法永远不会抛出 ValueError
。
我们可以使用相同的方法来获取字典中值的索引。
my_dict = {
'site': 'jiyik.com',
'color': 'blue',
'number': 100
}
index = None
dict_values = my_dict.values()
if 'jiyik.com' in dict_values:
index = list(dict_values).index('jiyik.com')
print(index) # 👉️ 0
从 Python 3.7 开始,标准 dict 类保证保留键的插入顺序。
如果是使用旧版本,请改用 OrderedDict
类。
from collections import OrderedDict
my_dict = OrderedDict(
[('site', 'jiyik.com'), ('color', 'blue'), ('number', 100)]
)
key = list(my_dict)[0]
print(key) # 👉️ site
value = list(my_dict.values())[0]
print(value) # 👉️ jiyik.com
# --------------------------------------
index = None
if 'site' in my_dict:
index = list(my_dict).index('site')
print(index) # 👉️ 0
list()
类也可用于将 OrderedDict
的键转换为列表。
请注意
,仅当我们使用 Python 3.7 之前的版本时才需要使用 OrderedDict 类。
否则,请使用本机 dict
类,因为它保留了插入顺序。
相关文章
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中将 Timedelta 转换为 Int
发布时间:2024/04/23 浏览次数:231 分类:Python
-
可以使用 Pandas 中的 dt 属性将 timedelta 转换为整数。
Python 中的 Pandas 插入方法
发布时间:2024/04/23 浏览次数:112 分类:Python
-
本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。
使用 Python 将 Pandas DataFrame 保存为 HTML
发布时间:2024/04/21 浏览次数:106 分类:Python
-
本教程演示如何将 Pandas DataFrame 转换为 Python 中的 HTML 表格。
如何将 Python 字典转换为 Pandas DataFrame
发布时间:2024/04/20 浏览次数:73 分类:Python
-
本教程演示如何将 python 字典转换为 Pandas DataFrame,例如使用 Pandas DataFrame 构造函数或 from_dict 方法。
如何在 Pandas 中将 DataFrame 列转换为日期时间
发布时间:2024/04/20 浏览次数:101 分类:Python
-
本文介绍如何将 Pandas DataFrame 列转换为 Python 日期时间。