问题描述
我是Python的新手.对于经验丰富的开发人员和编码人员来说,我的问题可能很简单,但是我一直无法找到答案.
I am new to Python. My question might come across as simple to experienced developers and coders but I haven't been able to find out an answer to this.
我正在通过数据库查询检索一些数据.我已经成功地将查询返回的每一行组织为一个字典,该字典具有三个键和每个键的对应值.本质上,我已将查询返回的所有数据行整理到词典列表中.
I am retrieving some data through a database query. I have succeeded in organising each row returned by query as a dictionary having three keys and a corresponding value to each key. Essentially, I have organised all rows of data returned by the query into a list of dictionaries.
字典列表看起来像这样:
The list of dictionaries looks somewhat like this:
[
{'Date': date1, 'price': price1, 'itemnumber': number1},
{'Date': date2, 'price': price2, 'itemnumber': number2},
...
]
如何将此词典列表转换为与每个键相对应的三个单独的列表,即Date
,Price
和itemnumber
?
How can I convert this list of dictionaries into three separate lists corresponding to each key i.e. Date
, Price
and itemnumber
?
推荐答案
# Defining lists
dates = []
prices = []
item_numbers = []
# Loop through list
for dictionary in your_list:
dates.append(dictionary["Date"]) # Add the date to dates
prices.append(dictionary["price"]) # Add the price to prices
# Add item number to item_numbers
item_numbers.append(dictionary["itemnumber"])
这篇关于如何从字典列表中创建三个单独的值列表,其中每个词典都有三个键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!