📜  Python – 将字典列表转换为列表字典

📅  最后修改于: 2022-05-13 01:55:16.090000             🧑  作者: Mango

Python – 将字典列表转换为列表字典

在本文中,我们将讨论如何将字典列表转换为列表字典。

方法一:使用for循环

通过基于第一个键进行迭代,我们可以将字典列表转换为列表字典。用于创建学生词典列表的Python程序

Python3
# create a list of dictionaries
# with student data
data = [
    {'name': 'sravan', 'subjects': ['java', 'python']},
    {'name': 'bobby', 'subjects': ['c/cpp', 'java']},
    {'name': 'ojsawi', 'subjects': ['iot', 'cloud']},
    {'name': 'rohith', 'subjects': ['php', 'os']},
    {'name': 'gnanesh', 'subjects': ['html', 'sql']}
 
]
 
# display
data


Python3
# create an empty dictionary
dictionary_of_list = {}
 
# for loop to convert list of dict
# to dict of list
for item in data:
    name = item['name']
    dictionary_of_list[name] = item
 
# display
dictionary_of_list


Python3
# consider the list of dictionary
data = [{'manoj': 'java', 'bobby': 'python'},
        {'manoj': 'php', 'bobby': 'java'},
        {'manoj': 'cloud', 'bobby': 'big-data'}]
 
# convert into dictionary of list
# with list as values
print({key: [i[key] for i in data] for key in data[0]})


Python3
#import pandas
import pandas as pd
 
# consider the list of dictionary
data = [{'manoj': 'java', 'bobby': 'python'},
        {'manoj': 'php', 'bobby': 'java'},
        {'manoj': 'cloud', 'bobby': 'big-data'}]
 
# convert into dictionary of list
# with list as values using pandas dataframe
pd.DataFrame(data).to_dict(orient="list")


Python3
# import numpy module
import numpy as np
 
# create numpy data
data = np.array([(10, 20), (50, 60)],
                dtype=[('value1', int),
                       ('value2', int)])
 
# display by using index
print(data[0])
 
# display by using key
print(data['value1'])


输出:

[{'name': 'sravan', 'subjects': ['java', 'python']},
 {'name': 'bobby', 'subjects': ['c/cpp', 'java']},
 {'name': 'ojsawi', 'subjects': ['iot', 'cloud']},
 {'name': 'rohith', 'subjects': ['php', 'os']},
 {'name': 'gnanesh', 'subjects': ['html', 'sql']}]

示例:将字典列表转换为列表字典

Python3

# create an empty dictionary
dictionary_of_list = {}
 
# for loop to convert list of dict
# to dict of list
for item in data:
    name = item['name']
    dictionary_of_list[name] = item
 
# display
dictionary_of_list

输出:

{'bobby': {'name': 'bobby', 'subjects': ['c/cpp', 'java']},
 'gnanesh': {'name': 'gnanesh', 'subjects': ['html', 'sql']},
 'ojsawi': {'name': 'ojsawi', 'subjects': ['iot', 'cloud']},
 'rohith': {'name': 'rohith', 'subjects': ['php', 'os']},
 'sravan': {'name': 'sravan', 'subjects': ['java', 'python']}}

方法2:使用字典理解

在这里,我们使用字典推导将字典列表转换为列表的字典,因此我们将列表作为基于新字典中的键的值传递

示例:将学生字典列表转换为列表字典的程序

Python3

# consider the list of dictionary
data = [{'manoj': 'java', 'bobby': 'python'},
        {'manoj': 'php', 'bobby': 'java'},
        {'manoj': 'cloud', 'bobby': 'big-data'}]
 
# convert into dictionary of list
# with list as values
print({key: [i[key] for i in data] for key in data[0]})

输出:

{'manoj': ['java', 'php', 'cloud'], 'bobby': ['python', 'java', 'big-data']}

方法3:使用pandas DataFrame

通过使用 pandas 数据框,我们将 dict 列表转换为 dict 列表

示例

Python3

#import pandas
import pandas as pd
 
# consider the list of dictionary
data = [{'manoj': 'java', 'bobby': 'python'},
        {'manoj': 'php', 'bobby': 'java'},
        {'manoj': 'cloud', 'bobby': 'big-data'}]
 
# convert into dictionary of list
# with list as values using pandas dataframe
pd.DataFrame(data).to_dict(orient="list")

输出

方法 4:使用 NumPy

Numpy 用于处理数组,我们可以通过键或索引来获取值

Python3

# import numpy module
import numpy as np
 
# create numpy data
data = np.array([(10, 20), (50, 60)],
                dtype=[('value1', int),
                       ('value2', int)])
 
# display by using index
print(data[0])
 
# display by using key
print(data['value1'])

输出:

(10, 20)
[10 50]