Python字典items()方法与示例

词典items()方法

items()方法用于获取所有项作为视图对象,该视图对象表示字典的键值对。

语法:

    dictionary_name.items()

Parameter(s):

  • 它不接受任何参数。

返回值:

此方法的返回类型为<class'dict_items'>,它将字典的项作为视图对象返回。

示例

# Python字典items()方法与示例

# 字典声明
student = {
  "roll_no": 101,
  "name": "Shivang",
  "course": "B.Tech",
  "perc" : 98.5
}

# 印刷词典
print("data of student dictionary...")
print(student)

# 印刷品
print("items of student dictionary...")
print(student.items())

# 学生的打印返回类型.items()方法
print("return type is: ", type(student.items()))

输出结果

data of student dictionary...
{'name': 'Shivang', 'perc': 98.5, 'roll_no': 101, 'course': 'B.Tech'}
items of student dictionary...
dict_items([('name', 'Shivang'), ('perc', 98.5), ('roll_no', 101), ('course', 'B.Tech')])
return type is:  <class 'dict_items'>