當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Dictionary values()用法及代碼示例

字典 values() 方法

values() 方法用於獲取字典的所有值,它返回一個視圖對象,該對象包含字典的所有值作為列表。

用法:

    dictionary_name.values()

參數:

  • 它不接受任何參數。

返回值:

這個方法的返回類型是<class 'dict_values'>,它將所有值作為包含所有值列表的視圖對象返回。

例:

# Python Dictionary values() Method with Example

# dictionary declaration
student = {
  "roll_no":101,
  "name":"Shivang",
  "course":"B.Tech",
  "perc":98.5
}

# printing dictionary
print("data of student dictionary...")
print(student)

# getting all values
x = student.values()
print(x)

# printing type of values() Method
print('Type is:',type(student.values()))

# changing the value
# it will effect the value of view object also
student['course'] = 'MCA'

# printing dictionary
print("data of student dictionary...")
print(student)

# getting all values
x = student.values()
print(x)

輸出

data of student dictionary...
{'roll_no':101, 'name':'Shivang', 'course':'B.Tech', 'perc':98.5}
dict_values([101, 'Shivang', 'B.Tech', 98.5])
Type is: <class 'dict_values'>
data of student dictionary...
{'roll_no':101, 'name':'Shivang', 'course':'MCA', 'perc':98.5}
dict_values([101, 'Shivang', 'MCA', 98.5])


相關用法


注:本文由純淨天空篩選整理自 Python Dictionary values() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。