Python item() 方法返回字典的新視圖。此視圖是鍵值元組的集合。此方法不帶任何參數,如果字典為空則返回空視圖。下麵給出了示例和語法。
簽名
items()
參數
沒有參數
返回
它返回字典的視圖。
讓我們看一些 items() 方法的例子來了解它的函數。
Python字典items()方法示例1
這是一個簡單的例子,它返回字典中存在的所有項目。
# Python dictionary items() Method
# Creating a dictionary
student = {'name':'rohan', 'course':'B.Tech', 'email':'rohan@abc.com'}
# Calling function
items = student.items()
# Displaying result
print(items)
輸出:
dict_items([('name', 'rohan'), ('course', 'B.Tech'), ('email', '[email protected]')])
Python字典items()方法示例2
如果字典已經為空,則此方法不會引發任何錯誤。請參閱下麵的示例。
# Python dictionary items() Method
# Creating a dictionary
student = {} # dictionary is empty
# Calling function
items = student.items()
# Displaying result
print(items)
輸出:
dict_items([])
Python字典items()方法示例3
除了 items() 方法,我們還可以使用其他自定義方法來獲取字典元素。請參閱下麵的示例。
# Python dictionary items() Method
# Creating a dictionary
student = {'name':'rohan', 'course':'B.Tech', 'email':'rohan@abc.com'}
# Iterating using key and value
for st in student:
print("(",st, ":", student[st], end="), ")
# Calling function
items = student.items()
# Displaying result
print("\n", items)
輸出:
( name:rohan), ( course:B.Tech), ( email:[email protected]), dict_items([('name', 'rohan'), ('course', 'B.Tech'), ('email', '[email protected]')])
相關用法
- Python Dictionary items()用法及代碼示例
- 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 copy()用法及代碼示例
- Python Dictionary keys()用法及代碼示例
- Python Dictionary values()用法及代碼示例
- 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 items() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。