Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
This problem can be solved using the
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 10 function which does the task of extracting a key-value pair and using the 0th index gives us the first key-value pair. Works only in Python2.
Đôi khi, trong khi làm việc với từ điển, chúng ta có thể phải đối mặt với một vấn đề trong đó chúng ta có thể chỉ có một từ điển đơn lẻ, tức là từ điển chỉ với một cặp giá trị khóa duy nhất và yêu cầu để có được cặp theo các biến riêng biệt. Loại vấn đề này có thể đến trong lập trình hàng ngày. Hãy để thảo luận về những cách nhất định trong đó điều này có thể được thực hiện.
Phương pháp số 1: Sử dụng vấn đề items[]
này có thể được giải quyết bằng cách sử dụng hàm
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 10, thực hiện nhiệm vụ trích xuất một cặp giá trị khóa và sử dụng chỉ số 0 cho chúng ta cặp giá trị khóa đầu tiên. Chỉ hoạt động trong Python2.
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 11
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 12
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 13
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 14
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 15
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 16
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 17
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 18
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 19
d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
0 ________ 21 & nbsp; d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
2d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
3d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
4The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 12
d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
6d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
7d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
8The original dictionary : {'gfg': 1}
The 1st key of dictionary is : gfg
The 1st value of dictionary is : 1
8The original dictionary : {'gfg': 1}
The 1st key of dictionary is : gfg
The 1st value of dictionary is : 1
9
1 d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
1 d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
2d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
4d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 1
The original dictionary : {'gfg': 1}
The 1st key of dictionary is : gfg
The 1st value of dictionary is : 1
8The original dictionary : {'gfg': 1}
The 1st key of dictionary is : gfg
The 1st value of dictionary is : 1
9
7 d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
1 d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
2d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
0keys = [k for k, v in d.items[] if v == 'aaa']
print[keys]
# ['key1', 'key2']
keys = [k for k, v in d.items[] if v == 'bbb']
print[keys]
# ['key3']
keys = [k for k, v in d.items[] if v == 'xxx']
print[keys]
# []
The combination of above functions can be used to perform this particular task. It uses iterators to perform this task. The
keys = [k for k, v in d.items[] if v == 'aaa']
print[keys]
# ['key1', 'key2']
keys = [k for k, v in d.items[] if v == 'bbb']
print[keys]
# ['key3']
keys = [k for k, v in d.items[] if v == 'xxx']
print[keys]
# []
2 is
used to fetch the pairs till dictionary is exhausted. It works with Python3.Đôi khi, trong khi làm việc với từ điển, chúng ta có thể phải đối mặt với một vấn đề trong đó chúng ta có thể chỉ có một từ điển đơn lẻ, tức là từ điển chỉ với một cặp giá trị khóa duy nhất và yêu cầu để có được cặp theo các biến riêng biệt. Loại vấn đề này có thể đến trong lập trình hàng ngày. Hãy để thảo luận về những cách nhất định trong đó điều này có thể được thực hiện.
Phương pháp số 1: Sử dụng vấn đề items[]
này có thể được giải quyết bằng cách sử dụng hàm
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 10, thực hiện nhiệm vụ trích xuất một cặp giá trị khóa và sử dụng chỉ số 0 cho chúng ta cặp giá trị khóa đầu tiên. Chỉ hoạt động trong Python2.
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 11
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 12
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 13
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 14
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 15
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 16
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 17
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 18
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 19
d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
0 ________ 21 & nbsp; d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
2d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
3d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
4The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 12
d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
6d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
7d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
8The original dictionary : {'gfg': 1}
The 1st key of dictionary is : gfg
The 1st value of dictionary is : 1
8The original dictionary : {'gfg': 1}
The 1st key of dictionary is : gfg
The 1st value of dictionary is : 1
9
1 d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
1 d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
2d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
4d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
The original dictionary : {'gfg': 1} The 1st key of dictionary is : gfg The 1st value of dictionary is : 1
Bài viết này mô tả cách lấy khóa từ giá trị trong một từ điển [đối tượng loại
def get_key_from_value[d, val]:
keys = [k for k, v in d.items[] if v == val]
if keys:
return keys[0]
return None
key = get_key_from_value[d, 'aaa']
print[key]
# key1
key = get_key_from_value[d, 'bbb']
print[key]
# key3
key = get_key_from_value[d, 'xxx']
print[key]
# None
4] trong Python.- Nhận khóa từ giá trị với danh sách hiểu và
items[]
- Mã mẫu để trích xuất các khóa với các điều kiện khác nhau
Để có được giá trị bằng khóa, chỉ cần chỉ định khóa như sau.
d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
value = d['key1']
print[value]
# aaa
Bạn cũng có thể sử dụng phương thức
def get_key_from_value[d, val]:
keys = [k for k, v in d.items[] if v == val]
if keys:
return keys[0]
return None
key = get_key_from_value[d, 'aaa']
print[key]
# key1
key = get_key_from_value[d, 'bbb']
print[key]
# key3
key = get_key_from_value[d, 'xxx']
print[key]
# None
6 để lấy giá trị bằng khóa. Nếu bạn sử dụng phương thức def get_key_from_value[d, val]:
keys = [k for k, v in d.items[] if v == val]
if keys:
return keys[0]
return None
key = get_key_from_value[d, 'aaa']
print[key]
# key1
key = get_key_from_value[d, 'bbb']
print[key]
# key3
key = get_key_from_value[d, 'xxx']
print[key]
# None
6, không có lỗi nào được nêu ra ngay cả khi bạn chỉ định khóa không tồn tại. Xem bài viết sau đây.- Nhận giá trị từ từ điển theo khóa với get [] trong python
Bạn cũng có thể sử dụng phương thức
def get_key_from_value[d, val]:
keys = [k for k, v in d.items[] if v == val]
if keys:
return keys[0]
return None
key = get_key_from_value[d, 'aaa']
print[key]
# key1
key = get_key_from_value[d, 'bbb']
print[key]
# key3
key = get_key_from_value[d, 'xxx']
print[key]
# None
8 để có được danh sách tất cả các khóa.- Lặp lại từ điển [khóa và giá trị] với vòng lặp trong Python
Nhận khóa từ giá trị với danh sách hiểu và các mục []
Để có được khóa từ giá trị trong từ điển, hãy sử dụng phương thức hiểu danh sách và phương thức items[]
.
Để biết danh sách toàn diện và các vòng lặp
d_num = {'key1': 1, 'key2': 2, 'key3': 3}
keys = [k for k, v in d_num.items[] if v >= 2]
print[keys]
# ['key2', 'key3']
keys = [k for k, v in d_num.items[] if v % 2 == 1]
print[keys]
# ['key1', 'key3']
d_str = {'key1': '', 'key2': '', 'key3': ''}
keys = [k for k, v in d_str.items[] if v.endswith['com']]
print[keys]
# ['key1', 'key3']
0 cho từ điển, hãy xem các bài viết sau.- Liệt kê sự hiểu biết trong Python
- Lặp lại từ điển [khóa và giá trị] với vòng lặp trong Python
Nhận khóa từ giá trị với danh sách hiểu và các mục []
d = {'key1': 'aaa', 'key2': 'aaa', 'key3': 'bbb'}
keys = [k for k, v in d.items[] if v == 'aaa']
print[keys]
# ['key1', 'key2']
keys = [k for k, v in d.items[] if v == 'bbb']
print[keys]
# ['key3']
keys = [k for k, v in d.items[] if v == 'xxx']
print[keys]
# []
Để có được khóa từ giá trị trong từ điển, hãy sử dụng phương thức hiểu danh sách và phương thức items[]
.
Để biết danh sách toàn diện và các vòng lặp
d_num = {'key1': 1, 'key2': 2, 'key3': 3}
keys = [k for k, v in d_num.items[] if v >= 2]
print[keys]
# ['key2', 'key3']
keys = [k for k, v in d_num.items[] if v % 2 == 1]
print[keys]
# ['key1', 'key3']
d_str = {'key1': '', 'key2': '', 'key3': ''}
keys = [k for k, v in d_str.items[] if v.endswith['com']]
print[keys]
# ['key1', 'key3']
0 cho từ điển, hãy xem các bài viết sau.Liệt kê sự hiểu biết trong Python
Sau đây là mã mẫu để có được danh sách các khóa được ghép nối với một giá trị được chỉ định. Nếu khóa có giá trị được chỉ định không tồn tại, một danh sách trống sẽ được trả về.
def get_keys_from_value[d, val]:
return [k for k, v in d.items[] if v == val]
keys = get_keys_from_value[d, 'aaa']
print[keys]
# ['key1', 'key2']
Đối với các từ điển không có giá trị trùng lặp, hàm sau cũng có thể.
Nếu một khóa có giá trị được chỉ định tồn tại, khóa đó được trả về, nếu không
d_num = {'key1': 1, 'key2': 2, 'key3': 3}
keys = [k for k, v in d_num.items[] if v >= 2]
print[keys]
# ['key2', 'key3']
keys = [k for k, v in d_num.items[] if v % 2 == 1]
print[keys]
# ['key1', 'key3']
d_str = {'key1': '', 'key2': '', 'key3': ''}
keys = [k for k, v in d_str.items[] if v.endswith['com']]
print[keys]
# ['key1', 'key3']
4 sẽ được trả về. Nếu các giá trị được sao chép, một trong các khóa được trả về.def get_key_from_value[d, val]:
keys = [k for k, v in d.items[] if v == val]
if keys:
return keys[0]
return None
key = get_key_from_value[d, 'aaa']
print[key]
# key1
key = get_key_from_value[d, 'bbb']
print[key]
# key3
key = get_key_from_value[d, 'xxx']
print[key]
# None
Trong ví dụ trên, các khóa có giá trị bằng giá trị được chỉ định được trích xuất.
Bạn có thể trích xuất các khóa trong các điều kiện khác nhau bằng cách thay đổi phần biểu thức có điều kiện của danh sách hiểu.
d_num = {'key1': 1, 'key2': 2, 'key3': 3}
keys = [k for k, v in d_num.items[] if v >= 2]
print[keys]
# ['key2', 'key3']
keys = [k for k, v in d_num.items[] if v % 2 == 1]
print[keys]
# ['key1', 'key3']
d_str = {'key1': '', 'key2': '', 'key3': ''}
keys = [k for k, v in d_str.items[] if v.endswith['com']]
print[keys]
# ['key1', 'key3']