Phương pháp từ điển
Thí dụ
Trả về các giá trị:
xe = {& nbsp; "Thương hiệu": "Ford", & NBSP; "Mô hình": "Mustang", & nbsp; "Năm": 1964}
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.values ()
print(x)
Hãy tự mình thử »
Định nghĩa và cách sử dụng
Phương thức dictionary.values()1 trả về một đối tượng xem. Đối tượng xem chứa các giá trị của từ điển, như một danh sách.
Đối tượng xem sẽ phản ánh bất kỳ thay đổi nào được thực hiện cho từ điển, xem ví dụ bên dưới.
Cú pháp
Giá trị tham số
Không có tham số
Nhiều ví dụ hơn
Thí dụ
Khi một giá trị được thay đổi trong từ điển, đối tượng xem cũng được cập nhật:
xe = {& nbsp; "Thương hiệu": "Ford", & NBSP; "Mô hình": "Mustang", & nbsp; "Năm": 1964}
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.values ()
Hãy tự mình thử »
Định nghĩa và cách sử dụng
Hãy tự mình thử »
Phương pháp từ điển
Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương thức từ điển Python () với sự trợ giúp của các ví dụ.
Phương thức dictionary.values()1 trả về một đối tượng xem hiển thị danh sách tất cả các giá trị trong từ điển.
Thí dụ
marks = {'Physics':67, 'Maths':87}print(marks.values())
# Output: dict_values([67, 87])Cú pháp của các giá trị từ điển ()
Cú pháp của dictionary.values()1 là:
dictionary.values()giá trị () tham số
Phương thức dictionary.values()1 không lấy bất kỳ tham số nào.
Trả về giá trị từ giá trị ()
Phương thức dictionary.values()1 Trả về một đối tượng xem hiển thị danh sách tất cả các giá trị trong một từ điển nhất định.
Ví dụ 1: Nhận tất cả các giá trị từ từ điển
# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }print(sales.values())
Đầu ra
dict_values([2, 4, 3])Ví dụ 2: Làm thế nào các giá trị () hoạt động khi một từ điển được sửa đổi?
# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }values = sales.values()
print('Original items:', values) # delete an item from dictionarydel[sales['apple']]
print('Updated items:', values)Đầu ra
Original items: dict_values([2, 4, 3]) Updated items: dict_values([4, 3])Ví dụ 2: Làm thế nào các giá trị () hoạt động khi một từ điển được sửa đổi?
Các giá trị đối tượng xem không tự trả về danh sách các giá trị mục bán hàng nhưng nó trả về chế độ xem tất cả các giá trị của từ điển.
Giá trị và loại:
Giá trị là một trong những điều cơ bản nhất trong bất kỳ chương trình nào hoạt động. Một giá trị có thể là các ký tự, tức là Hello, thế giới! Hay hoặc một số như 1.2.2, 3.5, v.v.
Đầu tiên, chúng tôi nhập lệnh Python để bắt đầu trình thông dịch.
Python hỗ trợ 3 loại số: số nguyên, float và số phức. & Nbsp; nếu bạn muốn biết & nbsp; giá trị loại A nào bạn có thể sử dụng hàm loại (). Dán mã sau và nhấp vào nút chạy để kiểm tra đầu ra.
Hãy tự mình thử nóStrings:dictionary.values()6
Chuỗi được xác định với một trích dẫn hoặc một trích dẫn kép. Sự khác biệt giữa hai là sử dụng dấu ngoặc kép giúp dễ dàng bao gồm các dấu nháy đơn.
Hãy tự mình thử nódictionary.values()7
Variables:
Chuỗi được xác định với một trích dẫn hoặc một trích dẫn kép. Sự khác biệt giữa hai là sử dụng dấu ngoặc kép giúp dễ dàng bao gồm các dấu nháy đơn.
dictionary.values()8
dictionary.values()7
print(id)
print(height)
Một biến không có gì ngoài một cái tên đề cập đến một giá trị. & Nbsp; một câu lệnh gán tạo ra các biến mới và cung cấp cho chúng các giá trị.
in (tên) in (id) in (chiều cao)
print(type(id))
print(type(height))
Loại biến có nghĩa là loại giá trị mà nó đề cập đến.
Hãy tự mình thử nódictionary.values()9
Cải thiện bài viết
Lưu bài viết
Giá trị () là một phương thức sẵn có trong ngôn ngữ lập trình Python trả về đối tượng Chế độ xem. Đối tượng xem chứa các giá trị của từ điển, như một danh sách. Nếu bạn sử dụng phương thức loại () trên giá trị trả về, bạn sẽ nhận được đối tượng Dict_Values đối tượng. Nó phải được đúc để có được danh sách thực tế. is an inbuilt method in Python programming language that returns a view object. The view object contains the values of the dictionary, as a list. If you use the type() method on the return value, you get “dict_values object”. It must be cast to obtain the actual list.
Syntax:
dictionary_name.values()Tham số: không có tham số
There are no
parameters
Returns:
returns a list of all the values available in a given dictionary. the values have been stored in a reversed manner.Error:
As we are not passing any parameters there is no scope for any error.Code#1:
# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }
print(sales.values())
0# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }print(sales.values())
1 # random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }print(sales.values())
2# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }print(sales.values())
3# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }print(sales.values())
4# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }print(sales.values())
5____26# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }print(sales.values())
7# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }print(sales.values())
4# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }print(sales.values())
9# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }print(sales.values())
6dict_values([2, 4, 3])1# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }print(sales.values())
4dict_values([2, 4, 3])3dict_values([2, 4, 3])4dict_values([2, 4, 3])5dict_values([2, 4, 3])6
# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }
print(sales.values())
0# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }print(sales.values())
1 # random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }print(sales.values())
2# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }values = sales.values()
print('Original items:', values) # delete an item from dictionarydel[sales['apple']]
print('Updated items:', values)0# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }print(sales.values())
4# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }values = sales.values()
print('Original items:', values) # delete an item from dictionarydel[sales['apple']]
print('Updated items:', values)2# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }print(sales.values())
6__dict_values([2, 4, 3])5dict_values([2, 4, 3])6
Output:
dict_values([2, 3, 4]) dict_values(['5', '3', '5'])Ứng dụng thực tế: Được đặt tên và tiền lương, trả lại tổng lương của tất cả nhân viên.
Given name and salary, return the total salary of all employees.
Code#2:
Original items: dict_values([2, 4, 3]) Updated items: dict_values([4, 3])4# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }
print(sales.values())
1 # random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }print(sales.values())
22____23dictionary_name.values()9# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 }
print(sales.values())
1 returns a list of all the values available in a given dictionary. the values have been stored in a reversed manner.1dict_values([2, 4, 3])5returns a list of all the values available in a given dictionary. the values have been stored in a reversed manner.3returns a list of all the values available in a given dictionary. the values have been stored in a reversed manner.4returns a list of all the values available in a given dictionary. the values have been stored in a reversed manner.5
Output:
dictionary.values()0