在 Python 中获取 OrderedDict 的第一个元素

在 Python 中获取 OrderedDict 的第一个元素

Get the First element of an OrderedDict in Python

获取 an 的第一个元素OrderedDict

  1. 使用该iter()函数从
    OrderedDict.
  2. 将迭代器对象传递给next()函数以获取OrderedDict.
主程序
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) first_key = next(iter(my_dict)) print(first_key) # 👉️ name first_value = my_dict[first_key] print(first_value) # 👉️ bobbyhadz first_pair = next(iter(my_dict.items())) print(first_pair) # 👉️ ('name', 'bobbyhadz') print(list(my_dict.items())[0]) # 👉️ ('name', 'bobbyhadz') print(list(my_dict.items())[1]) # 👉️ ('age', 30)

iter函数接受一个对象并从中构造一个迭代器

next()函数从提供的迭代器返回下一个项目。

如果dict.items()需要获取. iter()OrderedDict
主程序
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) first_key = next(iter(my_dict)) print(first_key) # 👉️ name first_value = my_dict[first_key] print(first_value) # 👉️ bobbyhadz first_pair = next(iter(my_dict.items())) print(first_pair) # 👉️ ('name', 'bobbyhadz')

dict.items方法返回字典
项((键,值)对)的新视图。

主程序
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) # 👇️ odict_items([('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')]) print(my_dict.items())

如果需要访问 中的特定索引,可以将视图对象转换为列表OrderedDict

主程序
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) print(list(my_dict.items())[0]) # 👉️ ('name', 'bobbyhadz') print(list(my_dict.items())[1]) # 👉️ ('age', 30)
将视图对象转换为列表是必要的,因为视图对象不可订阅(无法在特定索引处访问)。

如果您reversed()需要获取
OrderedDict.

主程序
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) last_key = next(reversed(my_dict)) print(last_key) # 👉️ topic last_value = my_dict[last_key] print(last_value) # 👉️ Python last_pair = next(reversed(my_dict.items())) print(last_pair) # 👉️ ('topic', 'Python')

reversed函数接受一个迭代器,将其
反转并返回结果。

主程序
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) # 👇️ ['topic', 'age', 'name'] print(list(reversed(my_dict)))

请注意,该next()函数可以传递一个默认值作为第二个参数。

主程序
from collections import OrderedDict my_dict = OrderedDict() first_pair = next(iter(my_dict.items()), None) print(first_pair) # 👉️ None if first_pair is not None: print('Do work')

如果迭代器耗尽或为空,则返回默认值。

如果迭代器耗尽或为空且未提供默认值,StopIteration则会引发异常。

您还可以使用基本for循环来获取OrderedDict.

使用 for 循环获取 OrderedDict 的第一个元素#

获取 an 的第一个元素OrderedDict

  1. 使用for循环遍历OrderedDict.
  2. 将第一个键值对存储在变量中。
  3. 使用该break语句在第一次迭代时退出循环。
主程序
from collections import OrderedDict my_dict = OrderedDict( [('name', 'bobbyhadz'), ('age', 30), ('topic', 'Python')] ) for key in my_dict: print(key) # 👉️ name print(my_dict[key]) # 👉️ bobbyhadz break

我们使用for循环来迭代OrderedDict一次。

break
语句跳出最内层的封闭

for循环while

在我们得到 的第一个键值对后OrderedDict,我们退出for
循环。

OrderedDict
类是 的子类,

dict会记住其条目的添加顺序。

请注意,从 Python 3.7 开始,标准dict类也保证保留插入顺序。