Python values() 方法用於從字典中收集所有值。它不接受任何參數並返回值字典。如果字典沒有值,它返回一個空字典。下麵給出了該方法的語法和示例。
簽名
values()
參數
無參數
返回
它返回一個值字典。
讓我們看一些 values() 方法的例子來理解它的函數。
Python字典values()方法示例1
這是一個簡單的例子,它返回字典中的所有值。下麵給出一個例子。
# Python dictionary values() Method
# Creating a dictionary
einventory = {'Fan':200, 'Bulb':150, 'Led':1000}
# Calling function
stock = einventory.values()
# Displaying result
print("Stock available",einventory)
輸出:
Stock available {'Fan':200, 'Bulb':150, 'Led':1000}
Python字典values()方法示例2
如果字典已經是空的,這個方法也會返回一個空字典,除了任何錯誤或異常。請參閱下麵的示例。
# Python dictionary values() Method
# Creating a dictionary
einventory = {}
# Calling function
stock = einventory.values()
# Displaying result
print("Stock available",einventory)
輸出:
Stock available {}
Python字典values()方法示例3
此示例使用各種方法來獲取有關字典的信息,例如長度、鍵等。
# Python dictionary values() Method
# Creating a dictionary
einventory = {'Fan':200, 'Bulb':150, 'Led':1000}
# Applying functions
length = len(einventory)
print("Total number of values:",length)
keys = einventory.keys()
print("All the Keys:",keys)
item = einventory.items()
print("Items:",einventory)
p = einventory.popitem()
print("Deleted items:",p)
stock = einventory.values()
print("Stock available",einventory)
輸出:
Total number of values:3 All the Keys:dict_keys(['Fan', 'Bulb', 'Led']) Items:{'Fan':200, 'Bulb':150, 'Led':1000} Deleted items:('Led', 1000) Stock available {'Fan':200, 'Bulb':150}
相關用法
- Python Dictionary values()用法及代碼示例
- Python Dictionary fromkeys()用法及代碼示例
- Python Dictionary clear()用法及代碼示例
- Python Dictionary update()用法及代碼示例
- Python Dictionary pop()用法及代碼示例
- Python Dictionary setdefault()用法及代碼示例
- Python Dictionary popitem()用法及代碼示例
- Python Dictionary has_key()用法及代碼示例
- Python Dictionary get()用法及代碼示例
- Python Dictionary items()用法及代碼示例
- Python Dictionary copy()用法及代碼示例
- Python Dictionary keys()用法及代碼示例
- Python Decimal shift()用法及代碼示例
- Python Decimal next_plus()用法及代碼示例
- Python Decimal logical_and()用法及代碼示例
- Python Decimal rotate()用法及代碼示例
- Python Decimal max_mag()用法及代碼示例
- Python Datetime.replace()用法及代碼示例
- Python Decimal as_integer_ratio()用法及代碼示例
- Python DataFrame.to_excel()用法及代碼示例
注:本文由純淨天空篩選整理自 Python Dictionary values() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。