values()是Python編程語言中的內置方法,它返回給定詞典中所有可用值的列表。
用法:
dictionary_name.values()
參數:
沒有參數
返回:
returns a list of all the values available in a given dictionary. the values have been stored in a reversed manner.
錯誤:
As we are not passing any parameters there is no scope for any error.
代碼#1:
# Python3 program for illustration
# of values() method of dictionary
# numerical values
dictionary = {"raj":2, "striver":3, "vikram":4}
print(dictionary.values())
# string values
dictionary = {"geeks":"5", "for":"3", "geeks":"5"}
print(dictionary.values())
輸出:
dict_values([2, 3, 4]) dict_values(['5', '3'])
實際應用:
給定姓名和工資,返回所有雇員的總工資。
代碼#2:
# Python3 program for illustration
# of values() method in finding total salary
# stores name and corresponding salaries
salary = {"raj" :50000, "striver" :60000, "vikram" :5000}
# stores the salaries only
list1 = salary.values()
print(sum(list1)) # prints the sum of all salaries
輸出:
115000
注:本文由純淨天空篩選整理自Striver大神的英文原創作品 Python dictionary | values()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。