简体   繁体   English

用字典而不是python中的多个字典创建列表

[英]Creating list with dictionary instead of multiple dictionaries in python

I am having difficulties with the output format of the program 我在程序的输出格式上遇到困难

import pandas as pd

def main():
    data = pd.read_json("/home/mahmoud/Desktop/FB.json", orient='records')

    company_name = data['dataset']['name']
    columns_names = data['dataset']['column_names']
    stock_data = data['dataset']['data']

    data_list = []

    for rows in range(len(stock_data)):
        for columns in range(12):
            dataValues = {columns_names[columns]:stock_data[rows][columns]}
            data_list.append(dataValues)

    for val in range(len(data_list)):
        print(data_list[val])

if __name__=='__main__':main()

When i run this program I get the following output, every entry is a dictionary by itself: {'Date': '2018-12-11'} {'Open': 143.88} {'High': 143.88} {'Low': 141.1} {'Close': 142.08} {'Volume': 20300349.0} {'Dividend': 0.0} {'Split': 1.0} {'Adj_Open': 143.88} {'Adj_High': 143.88} {'Adj_Low': 141.1} {'Adj_Close': 142.08} 当我运行该程序时,得到以下输出,每个条目本身就是一个字典: {'Date': '2018-12-11'} {'Open': 143.88} {'High': 143.88} {'Low': 141.1} {'Close': 142.08} {'Volume': 20300349.0} {'Dividend': 0.0} {'Split': 1.0} {'Adj_Open': 143.88} {'Adj_High': 143.88} {'Adj_Low': 141.1} {'Adj_Close': 142.08}

I am trying to output the following: [{'Date':2018-12-11, 'Open':143.88, 'High':143.88, 'Low':141.1, 'Close':142.08, 'Volume':20300349.0, 'Dividend':0.0, 'Split':1.0, 'Adj_Open':143.88, 'Adj_High':143.88, 'Adj_Low':141.1, 'Adj_Close':142.08}, {'Date':22018-12-10, 'Open':139.6, 'High':143.05, 'Low':139.01, 'Close':141.85, 'Volume':26422173.0, 'Dividend':0.0, 'Split':1.0, 'Adj_Open':139.6, 'Adj_High':143.05, 'Adj_Low':139.01, 'Adj_Close':141.85}] 我正在尝试输出以下内容: [{'Date':2018-12-11, 'Open':143.88, 'High':143.88, 'Low':141.1, 'Close':142.08, 'Volume':20300349.0, 'Dividend':0.0, 'Split':1.0, 'Adj_Open':143.88, 'Adj_High':143.88, 'Adj_Low':141.1, 'Adj_Close':142.08}, {'Date':22018-12-10, 'Open':139.6, 'High':143.05, 'Low':139.01, 'Close':141.85, 'Volume':26422173.0, 'Dividend':0.0, 'Split':1.0, 'Adj_Open':139.6, 'Adj_High':143.05, 'Adj_Low':139.01, 'Adj_Close':141.85}]

These are the contents of the variables stock_data and columns_names 这些是变量stock_data和columns_names的内容

stock_data: [['2018-12-11', 143.88, 143.88, 141.1, 142.08, 20300349.0, 0.0, 1.0, 143.88, 143.88, 141.1, 142.08, 20300349.0], ['2018-12-10', 139.6, 143.05, 139.01, 141.85, 26422173.0, 0.0, 1.0, 139.6, 143.05, 139.01, 141.85]] stock_data: [['2018-12-11', 143.88, 143.88, 141.1, 142.08, 20300349.0, 0.0, 1.0, 143.88, 143.88, 141.1, 142.08, 20300349.0], ['2018-12-10', 139.6, 143.05, 139.01, 141.85, 26422173.0, 0.0, 1.0, 139.6, 143.05, 139.01, 141.85]]

columns_names: ['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Dividend', 'Split', 'Adj_Open', 'Adj_High', 'Adj_Low', 'Adj_Close', 'Adj_Volume'] column_names: ['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Dividend', 'Split', 'Adj_Open', 'Adj_High', 'Adj_Low', 'Adj_Close', 'Adj_Volume']

This can be as simple as using one list comprehension (the outter list) and one dictionary comprehension (the inner dicts). 这可以像使用一个列表理解(外部列表)和一个字典理解(内部字典)那样简单。 Just iterate over each row in stock_data and for each row iterate simultanously (using zip ) over the values of the current row and the columns names. 只需遍历stock_data每一行,并针对每一行(使用zip同时遍历当前行的值和列名。 All of that could be done using standard for loops, like in the code you posted, but that will be far more verbose; 所有这些都可以使用标准的for循环来完成,就像在您发布的代码中一样,但这将更加冗长; besides, the comprehensions below aren't that hard to read. 此外,下面的理解并不难读。

import pandas as pd

def main():
    data = pd.read_json("/home/mahmoud/Desktop/FB.json", orient='records')

    company_name = data['dataset']['name']
    columns_names = data['dataset']['column_names']
    stock_data = data['dataset']['data']

    # Here is the change
    data_list = [{c: v for c, v in zip(columns_names, r)} for r in stock_data]

    for val in data_list:
        print(val, data_list[val])

if __name__ == '__main__':
    main()

I hope this finally help you! 希望这对您有所帮助!

it's because you reset your dataValues after each iteration, so it's wiping out what was previously stored. 这是因为您在每次迭代后都会重置dataValues ,因此它会清除以前存储的内容。 You'll need to append the dataValues into your initial dictionary that you setup data_dict = {} . 您需要将dataValues附加到设置data_dict = {}初始字典中。 You never do anything with that in the code, I'm assuming thats where your trying to put all those value:keys 您永远不会对代码执行任何操作,我假设这就是您尝试放置所有这些值的地方:键

import pandas as pd

def main():

data = pd.read_json("/home/mahmoud/Desktop/FB.json", orient='records')

    company_name = data['dataset']['name']
    columns_names = data['dataset']['column_names']
    stock_data = data['dataset']['data']

    data_dict = {}

    for rows in range(len(stock_data)):
        for columns in range(12):
                dataValues = {column_name[columns]:stock_data[rows][columns]}
                # now take your dataValues, and add that to your data_dict below
                CODE GOES HERE

    for val in dataValues:
        print(val, dataValues[val])

if __name__ == '__main__':main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM