Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将元组中的数据提取到字典中?_Python_Python 3.x - Fatal编程技术网

Python 如何将元组中的数据提取到字典中?

Python 如何将元组中的数据提取到字典中?,python,python-3.x,Python,Python 3.x,我有一个元组: 项目=( (1,{“名称”:“计算机”,“价格”:20000,“数量”:5,“ed”:“个人”}), (2,{“名称”:“打印机”,“价格”:6000,“数量”:2,“ed”:“件”}), (3,{“名称”:“扫描仪”,“价格”:2000,“数量”:7,”ed:“件”。}) ) 我的任务是创建一个dict,如下所示: basket={ “名称”:[“计算机”、“打印机”、“扫描仪”], “价格”:[20000,6000,2000], “金额”:[5,2,7], “单位”:[“件

我有一个
元组

项目=(
(1,{“名称”:“计算机”,“价格”:20000,“数量”:5,“ed”:“个人”}),
(2,{“名称”:“打印机”,“价格”:6000,“数量”:2,“ed”:“件”}),
(3,{“名称”:“扫描仪”,“价格”:2000,“数量”:7,”ed:“件”。})
)
我的任务是创建一个
dict
,如下所示:

basket={
“名称”:[“计算机”、“打印机”、“扫描仪”],
“价格”:[20000,6000,2000],
“金额”:[5,2,7],
“单位”:[“件”]
}
我所做的:

我能够使用以下代码打印出我需要的数据:

converted_items=dict(items)
对于已转换项目中的项目:
打印(列表(已转换的项目[items].keys())[0])
对于转换的\u项[items]中的i,值()
印刷品(一)
使每个单词在新行中:

name
computer
20000
5
pcs
name
printer
6000
2
pcs and so on...

只需将元组转换为数据帧,然后再转换为字典 要转换为数据帧,请使用列表理解将元组转换为字典列表

items = ((1, {"name": "computer", "price": 20000, "quantity": 5, "ed": "pcs."}),
        (2, {"name": "printer", "price": 6000, "quantity": 2, "ed": "pcs."}),
        (3, {"name": "scanner", "price": 2000, "quantity": 7, "ed": "pcs."}))
df = pd.DataFrame([item[1] for item in items])
df.to_dict(orient='list')
输出

{'name': ['computer', 'printer', 'scanner'],
 'price': [20000, 6000, 2000],
 'quantity': [5, 2, 7],
 'ed': ['pcs.', 'pcs.', 'pcs.']}

只需将元组转换为数据帧,然后再转换为字典 要转换为数据帧,请使用列表理解将元组转换为字典列表

items = ((1, {"name": "computer", "price": 20000, "quantity": 5, "ed": "pcs."}),
        (2, {"name": "printer", "price": 6000, "quantity": 2, "ed": "pcs."}),
        (3, {"name": "scanner", "price": 2000, "quantity": 7, "ed": "pcs."}))
df = pd.DataFrame([item[1] for item in items])
df.to_dict(orient='list')
输出

{'name': ['computer', 'printer', 'scanner'],
 'price': [20000, 6000, 2000],
 'quantity': [5, 2, 7],
 'ed': ['pcs.', 'pcs.', 'pcs.']}

IMO
pandas
对于这样一个相对直截了当的事情来说完全是过火了——证据如下:

tems = (
    (1, {"name": "computer", "price": 20000, "quantity": 5, "ed": "pcs."}),
    (2, {"name": "printer", "price": 6000, "quantity": 2, "ed": "pcs."}),
    (3, {"name": "scanner", "price": 2000, "quantity": 7, "ed": "pcs."})
)
source_keys = 'name', 'price', 'quantity', 'ed'
target_keys = 'Name', 'Price', 'Amount', 'Unit'

basket = {key: [] for key in target_keys}  # Initialize to empty lists.
for _, item in items:
    for source_key, target_key in zip(source_keys, target_keys):
        basket[target_key].append(item[source_key])

from pprint import pprint
pprint(basket)
输出:

{'Amount': [5, 2, 7],
 'Name': ['computer', 'printer', 'scanner'],
 'Price': [20000, 6000, 2000],
 'Unit': ['pcs.', 'pcs.', 'pcs.']}

IMO
pandas
对于这样一个相对直截了当的事情来说完全是过火了——证据如下:

tems = (
    (1, {"name": "computer", "price": 20000, "quantity": 5, "ed": "pcs."}),
    (2, {"name": "printer", "price": 6000, "quantity": 2, "ed": "pcs."}),
    (3, {"name": "scanner", "price": 2000, "quantity": 7, "ed": "pcs."})
)
source_keys = 'name', 'price', 'quantity', 'ed'
target_keys = 'Name', 'Price', 'Amount', 'Unit'

basket = {key: [] for key in target_keys}  # Initialize to empty lists.
for _, item in items:
    for source_key, target_key in zip(source_keys, target_keys):
        basket[target_key].append(item[source_key])

from pprint import pprint
pprint(basket)
输出:

{'Amount': [5, 2, 7],
 'Name': ['computer', 'printer', 'scanner'],
 'Price': [20000, 6000, 2000],
 'Unit': ['pcs.', 'pcs.', 'pcs.']}
此特定情况下最短但最不可靠的解决方案 个人建议的更稳健的解决方案 深入解释 此特定情况下最短但最不可靠的解决方案 个人建议的更稳健的解决方案 深入解释