Hướng dẫn convert key-value to dictionary python - chuyển đổi khóa-giá trị thành từ điển python

Đôi khi, trong khi làm việc với các chuỗi Python, chúng ta có thể gặp vấn đề trong đó chúng ta cần chuyển đổi một cặp giá trị khóa chuỗi chuỗi thành từ điển. Điều này có thể có các ứng dụng trong đó chúng tôi đang làm việc với dữ liệu chuỗi cần được chuyển đổi. Hãy để thảo luận về những cách nhất định trong đó nhiệm vụ này có thể được thực hiện. & NBSP;

Phương thức số 1: Sử dụng map () + split () + loop & nbsp; 

Sự kết hợp của các chức năng trên có thể được sử dụng để thực hiện nhiệm vụ này. Trong đó, chúng tôi thực hiện chuyển đổi các cặp giá trị khóa thành từ điển bằng bản đồ và việc chia các cặp giá trị khóa được thực hiện bằng cách sử dụng split (). & Nbsp;

Python3

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
1
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
2
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
3

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
4
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
5
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
6
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
7
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
8
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
9

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
0
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
2
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
2

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
3
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
4
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
5
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
6
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
7
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
8

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
9
output = []
for key_value in lst:
    key, value = key_value.split(': ', 1)
    if not output or key in output[-1]:
        output.append({})
    output[-1][key] = value
0
output = []
for key_value in lst:
    key, value = key_value.split(': ', 1)
    if not output or key in output[-1]:
        output.append({})
    output[-1][key] = value
1
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
5
output = []
for key_value in lst:
    key, value = key_value.split(': ', 1)
    if not output or key in output[-1]:
        output.append({})
    output[-1][key] = value
3

output = []
for key_value in lst:
    key, value = key_value.split(': ', 1)
    if not output or key in output[-1]:
        output.append({})
    output[-1][key] = value
4
output = []
for key_value in lst:
    key, value = key_value.split(': ', 1)
    if not output or key in output[-1]:
        output.append({})
    output[-1][key] = value
5
output = []
for key_value in lst:
    key, value = key_value.split(': ', 1)
    if not output or key in output[-1]:
        output.append({})
    output[-1][key] = value
6
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
5
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
8
output = []
for key_value in lst:
    key, value = key_value.split(': ', 1)
    if not output or key in output[-1]:
        output.append({})
    output[-1][key] = value
9
output = []
for key_value in lst:
    key, value = key_value.split(': ', 1)
    if not output or key in output[-1]:
        output.append({})
    output[-1][key] = value
1
[{'name': 'test1',
  'email': '',
  'role': 'test',
  'description': 'test'},
 {'name': 'test2',
  'email': '',
  'role': 'test2',
  'description': 'test2'},
 {'name': 'test3',
  'email': '',
  'role': 'test3',
  'description': 'test3'}]
1
[{'name': 'test1',
  'email': '',
  'role': 'test',
  'description': 'test'},
 {'name': 'test2',
  'email': '',
  'role': 'test2',
  'description': 'test2'},
 {'name': 'test3',
  'email': '',
  'role': 'test3',
  'description': 'test3'}]
2
[{'name': 'test1',
  'email': '',
  'role': 'test',
  'description': 'test'},
 {'name': 'test2',
  'email': '',
  'role': 'test2',
  'description': 'test2'},
 {'name': 'test3',
  'email': '',
  'role': 'test3',
  'description': 'test3'}]
3

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
0
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
2
[{'name': 'test1',
  'email': '',
  'role': 'test',
  'description': 'test'},
 {'name': 'test2',
  'email': '',
  'role': 'test2',
  'description': 'test2'},
 {'name': 'test3',
  'email': '',
  'role': 'test3',
  'description': 'test3'}]
6
[{'name': 'test1',
  'email': '',
  'role': 'test',
  'description': 'test'},
 {'name': 'test2',
  'email': '',
  'role': 'test2',
  'description': 'test2'},
 {'name': 'test3',
  'email': '',
  'role': 'test3',
  'description': 'test3'}]
7

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
4
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
5
lst = ['name: test1', 'email: ', 'role: test', 'description: test', 
       'name: test2', 'email: ', 'role: test2', 'description: test2', 
       'name: test3', 'email: ', 'role: test3', 'description: test3']

answer = []

for i in range(0, len(lst), 4):
    dic = {}
    for j in lst[i:i+4]:
        dic[j.split(':')[0]] = j.split(':')[1].strip() 
    answer.append(dic)

# [{'name': 'test1',  'email': '',  'role': 'test',  'description': 'test'},
    #  {'name': 'test2',  'email': '',  'role': 'test2',  'description': 'test2'},
    #  {'name': 'test3',  'email': '',  'role': 'test3',  'description': 'test3'}]
0
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
7
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
8
lst = ['name: test1', 'email: ', 'role: test', 'description: test', 
       'name: test2', 'email: ', 'role: test2', 'description: test2', 
       'name: test3', 'email: ', 'role: test3', 'description: test3']

answer = []

for i in range(0, len(lst), 4):
    dic = {}
    for j in lst[i:i+4]:
        dic[j.split(':')[0]] = j.split(':')[1].strip() 
    answer.append(dic)

# [{'name': 'test1',  'email': '',  'role': 'test',  'description': 'test'},
    #  {'name': 'test2',  'email': '',  'role': 'test2',  'description': 'test2'},
    #  {'name': 'test3',  'email': '',  'role': 'test3',  'description': 'test3'}]
3

Đầu ra: & nbsp;

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}

Phương thức số 2: Sử dụng Dict () + Biểu thức máy phát + split () + map () & nbsp; 

Đây là một cách khác mà vấn đề này có thể được giải quyết. Trong đó, chúng tôi thực hiện nhiệm vụ theo cách tương tự như trên nhưng theo 1 cách sử dụng biểu thức Dict () và Trình tạo. & NBSP;

Python3

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
1
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
2
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
3

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
4
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
5
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
6
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
7
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
8
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
9

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
0
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
2
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
2

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
3
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
4
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
5
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
6
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
7
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
8

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
4
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
5
lst = ['name: test1', 'email: ', 'role: test', 'description: test', 
       'name: test2', 'email: ', 'role: test2', 'description: test2', 
       'name: test3', 'email: ', 'role: test3', 'description: test3']

answer = []

for i in range(0, len(lst), 4):
    dic = {}
    for j in lst[i:i+4]:
        dic[j.split(':')[0]] = j.split(':')[1].strip() 
    answer.append(dic)

# [{'name': 'test1',  'email': '',  'role': 'test',  'description': 'test'},
    #  {'name': 'test2',  'email': '',  'role': 'test2',  'description': 'test2'},
    #  {'name': 'test3',  'email': '',  'role': 'test3',  'description': 'test3'}]
0
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
7
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
8
lst = ['name: test1', 'email: ', 'role: test', 'description: test', 
       'name: test2', 'email: ', 'role: test2', 'description: test2', 
       'name: test3', 'email: ', 'role: test3', 'description: test3']

answer = []

for i in range(0, len(lst), 4):
    dic = {}
    for j in lst[i:i+4]:
        dic[j.split(':')[0]] = j.split(':')[1].strip() 
    answer.append(dic)

# [{'name': 'test1',  'email': '',  'role': 'test',  'description': 'test'},
    #  {'name': 'test2',  'email': '',  'role': 'test2',  'description': 'test2'},
    #  {'name': 'test3',  'email': '',  'role': 'test3',  'description': 'test3'}]
3

Đầu ra: & nbsp;

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}

Phương thức số 2: Sử dụng Dict () + Biểu thức máy phát + split () + map () & nbsp;

Đây là một cách khác mà vấn đề này có thể được giải quyết. Trong đó, chúng tôi thực hiện nhiệm vụ theo cách tương tự như trên nhưng theo 1 cách sử dụng biểu thức Dict () và Trình tạo. & NBSP;O(n)

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
0
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
2
[{'name': 'test1',
  'email': '',
  'role': 'test',
  'description': 'test'},
 {'name': 'test2',
  'email': '',
  'role': 'test2',
  'description': 'test2'},
 {'name': 'test3',
  'email': '',
  'role': 'test3',
  'description': 'test3'}]
6
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
5
output = []
for key_value in lst:
    key, value = key_value.split(': ', 1)
    if not output or key in output[-1]:
        output.append({})
    output[-1][key] = value
6
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
5
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
8
output = []
for key_value in lst:
    key, value = key_value.split(': ', 1)
    if not output or key in output[-1]:
        output.append({})
    output[-1][key] = value
9
output = []
for key_value in lst:
    key, value = key_value.split(': ', 1)
    if not output or key in output[-1]:
        output.append({})
    output[-1][key] = value
1
[{'name': 'test1',
  'email': '',
  'role': 'test',
  'description': 'test'},
 {'name': 'test2',
  'email': '',
  'role': 'test2',
  'description': 'test2'},
 {'name': 'test3',
  'email': '',
  'role': 'test3',
  'description': 'test3'}]
1
[{'name': 'test1',
  'email': '',
  'role': 'test',
  'description': 'test'},
 {'name': 'test2',
  'email': '',
  'role': 'test2',
  'description': 'test2'},
 {'name': 'test3',
  'email': '',
  'role': 'test3',
  'description': 'test3'}]
2
O(n)

dictionary = dict()
all_dictionaries = []
for index , value  in  [x.split(": ") for x in A] :
     if index in dictionary :
         all_dictionaries .append(dictionary )
         dictionary = dict()
     else :
       dictionary [index] = value
all_dictonaries.append(dictionary)
5
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
3
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
4
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
5

Độ phức tạp về thời gian và không gian cho tất cả các phương pháp là như nhau:

Độ phức tạp về thời gian: O (n)

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
1
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
2
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
3

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
4
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
5
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
6
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
7
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
8
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
9

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
0
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
2
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
2

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
3
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
4
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
5
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
6
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
7
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}
8

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
4
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
5
lst = ['name: test1', 'email: ', 'role: test', 'description: test', 
       'name: test2', 'email: ', 'role: test2', 'description: test2', 
       'name: test3', 'email: ', 'role: test3', 'description: test3']

answer = []

for i in range(0, len(lst), 4):
    dic = {}
    for j in lst[i:i+4]:
        dic[j.split(':')[0]] = j.split(':')[1].strip() 
    answer.append(dic)

# [{'name': 'test1',  'email': '',  'role': 'test',  'description': 'test'},
    #  {'name': 'test2',  'email': '',  'role': 'test2',  'description': 'test2'},
    #  {'name': 'test3',  'email': '',  'role': 'test3',  'description': 'test3'}]
0
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
7
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
8
lst = ['name: test1', 'email: ', 'role: test', 'description: test', 
       'name: test2', 'email: ', 'role: test2', 'description: test2', 
       'name: test3', 'email: ', 'role: test3', 'description: test3']

answer = []

for i in range(0, len(lst), 4):
    dic = {}
    for j in lst[i:i+4]:
        dic[j.split(':')[0]] = j.split(':')[1].strip() 
    answer.append(dic)

# [{'name': 'test1',  'email': '',  'role': 'test',  'description': 'test'},
    #  {'name': 'test2',  'email': '',  'role': 'test2',  'description': 'test2'},
    #  {'name': 'test3',  'email': '',  'role': 'test3',  'description': 'test3'}]
3

Đầu ra: & nbsp;

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'is': '2', 'gfg': '1', 'best': '3'}


Tôi muốn lặp lại danh sách này

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
27

và trả lại một danh sách các từ điển cho mỗi nhóm. Ví dụ.

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
28

Tôi đã thử phân tách danh sách theo, (dấu phẩy) và tìm kiếm nó cho 'Tên:'. Tôi có thể trả lại một lĩnh vực như tên, nhưng tôi đang đấu tranh để liên kết với email, vai trò, v.v.

Nhờ sự giúp đỡ trước.

Đã hỏi ngày 7 tháng 6 năm 2019 lúc 22:59Jun 7, 2019 at 22:59

Hướng dẫn convert key-value to dictionary python - chuyển đổi khóa-giá trị thành từ điển python

Leslie Alldridgeleslie AlldridgeLeslie Alldridge

1.2331 Huy hiệu vàng8 Huy hiệu bạc23 Huy hiệu đồng1 gold badge8 silver badges23 bronze badges

2

Không cần phải biết số lượng khóa mà mỗi Dict có trước, bạn có thể lặp lại qua danh sách, chia từng chuỗi thành một khóa và giá trị bằng và tiếp tục thêm giá trị vào dict cuối cùng bằng khóa:

output = []
for key_value in lst:
    key, value = key_value.split(': ', 1)
    if not output or key in output[-1]:
        output.append({})
    output[-1][key] = value

Vì vậy, trong danh sách mẫu của bạn được lưu trữ trong

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
30,
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
31 sẽ trở thành:

[{'name': 'test1',
  'email': '',
  'role': 'test',
  'description': 'test'},
 {'name': 'test2',
  'email': '',
  'role': 'test2',
  'description': 'test2'},
 {'name': 'test3',
  'email': '',
  'role': 'test3',
  'description': 'test3'}]

Đã trả lời ngày 7 tháng 6 năm 2019 lúc 23:11Jun 7, 2019 at 23:11

Hướng dẫn convert key-value to dictionary python - chuyển đổi khóa-giá trị thành từ điển python

0

Tôi giả sử rằng đơn đặt hàng của bạn luôn giống nhau, tức là, trong các nhóm 4. Ý tưởng là phân chia các chuỗi bằng cách sử dụng

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
32 và sau đó tạo các cặp khóa/giá trị và sử dụng lồng nhau cho các vòng lặp.
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
33 là để thoát khỏi khoảng trắngassuming that your order is always the same, i.e., in groups of 4. The idea is to split the strings using
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
32 and then create key/value pairs and use nested for loops. The
The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
33 is to get rid of whitespace

lst = ['name: test1', 'email: ', 'role: test', 'description: test', 
       'name: test2', 'email: ', 'role: test2', 'description: test2', 
       'name: test3', 'email: ', 'role: test3', 'description: test3']

answer = []

for i in range(0, len(lst), 4):
    dic = {}
    for j in lst[i:i+4]:
        dic[j.split(':')[0]] = j.split(':')[1].strip() 
    answer.append(dic)

# [{'name': 'test1',  'email': '',  'role': 'test',  'description': 'test'},
    #  {'name': 'test2',  'email': '',  'role': 'test2',  'description': 'test2'},
    #  {'name': 'test3',  'email': '',  'role': 'test3',  'description': 'test3'}]

Một danh sách hiểu sẽ trông giống như

answer = [{j.split(':')[0]:j.split(':')[1].strip() for j in lst[i:i+4]} for i in range(0, len(lst), 4)]

Đã trả lời ngày 7 tháng 6 năm 2019 lúc 23:09Jun 7, 2019 at 23:09

Hướng dẫn convert key-value to dictionary python - chuyển đổi khóa-giá trị thành từ điển python

SheldoresheldoreSheldore

36.3k6 Huy hiệu vàng48 Huy hiệu bạc64 Huy hiệu đồng6 gold badges48 silver badges64 bronze badges

Bạn có thể làm:

dictionary = dict()
all_dictionaries = []
for index , value  in  [x.split(": ") for x in A] :
     if index in dictionary :
         all_dictionaries .append(dictionary )
         dictionary = dict()
     else :
       dictionary [index] = value
all_dictonaries.append(dictionary)

Đã trả lời ngày 7 tháng 6 năm 2019 lúc 23:10Jun 7, 2019 at 23:10

Hướng dẫn convert key-value to dictionary python - chuyển đổi khóa-giá trị thành từ điển python

Ayoub Zarouayoub ZarouAyoub ZAROU

2.3775 huy hiệu bạc19 huy hiệu đồng5 silver badges19 bronze badges

Nếu hình thức của dữ liệu trong danh sách được đảm bảo luôn luôn là như vậy trong ví dụ của câu hỏi, thì bạn có thể làm điều này:

L = ['name: test1', 'email: ', 'role: test', 'description: test', 'name: test2', 'email: ', 'role: test2', 'description: test2', 'name: test3', 'email: ', 'role: test3', 'description: test3']

A = []

for i in range(0, len(L), 4):
  D = {}
  for p in L[i:i + 4]:
    k, v = map(str.strip, p.split(':'))
    D[k] = v
  A.append(D)

from pprint import pprint
pprint(A)

Output:

[{'description': 'test',
  'email': '',
  'name': 'test1',
  'role': 'test'},
 {'description': 'test2',
  'email': '',
  'name': 'test2',
  'role': 'test2'},
 {'description': 'test3',
  'email': '',
  'name': 'test3',
  'role': 'test3'}]

Đã trả lời ngày 7 tháng 6 năm 2019 lúc 23:12Jun 7, 2019 at 23:12

Hướng dẫn convert key-value to dictionary python - chuyển đổi khóa-giá trị thành từ điển python

DjaouadnmdjaouadnmDjaouadNM

21.6K4 Huy hiệu vàng29 Huy hiệu bạc53 Huy hiệu Đồng4 gold badges29 silver badges53 bronze badges

Giải pháp này giả định kích thước của mỗi nhóm là chính xác 4

The original string is : gfg:1, is:2, best:3
The converted dictionary is : {'gfg': '1', 'is': '2', 'best': '3'}
0

Đã trả lời ngày 7 tháng 6 năm 2019 lúc 23:23Jun 7, 2019 at 23:23

Hướng dẫn convert key-value to dictionary python - chuyển đổi khóa-giá trị thành từ điển python

GZ0GZ0GZ0

3,9221 Huy hiệu vàng9 Huy hiệu bạc20 Huy hiệu đồng 201 gold badge9 silver badges20 bronze badges

Làm thế nào để bạn chuyển đổi các giá trị thành một từ điển?

Để chuyển đổi các giá trị từ điển Python thành liệt kê, bạn có thể sử dụng phương thức Dict.Values ​​() trả về một đối tượng Dict_Values. Đối tượng này có thể được lặp lại và nếu bạn chuyển nó vào hàm tạo list (), nó sẽ trả về một đối tượng danh sách với các giá trị từ điển dưới dạng các phần tử.use dict. values() method which returns a dict_values object. This object can be iterated, and if you pass it to list() constructor, it returns a list object with dictionary values as elements.

Làm thế nào để bạn trao đổi một khóa

Bạn có thể trao đổi các khóa và giá trị trong từ điển với sự hiểu biết từ điển và phương thức mục () ...
Tạo một từ điển trong Python ({}, dict (), Dictorsions).
Lặp lại từ điển (khóa và giá trị) với vòng lặp trong Python ..

Làm thế nào để bạn chuyển đổi giữa các khóa và giá trị của từ điển trong Python?

My_dict là cấu trúc dữ liệu có thể lặp lại có thể là từ điển, danh sách hoặc một tuple.Trong trường hợp này, chúng ta cần thêm phương thức.Các mục () trả về một danh sách các cặp (giá trị, giá trị).my_dict.

Làm thế nào để từ điển nhận được giá trị từ khóa?

Bạn có thể sử dụng phương thức get () của từ điển (dict) để nhận bất kỳ giá trị mặc định nào mà không có lỗi nếu khóa không tồn tại.Chỉ định khóa là đối số đầu tiên.Giá trị tương ứng được trả về nếu khóa tồn tại và không có giá trị nào được trả về nếu khóa không tồn tại.use the get() method of the dictionary ( dict ) to get any default value without an error if the key does not exist. Specify the key as the first argument. The corresponding value is returned if the key exists, and None is returned if the key does not exist.