简体   繁体   中英

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}

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}]

These are the contents of the variables stock_data and 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]]

columns_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. All of that could be done using standard for loops, like in the code you posted, but that will be far more verbose; 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. You'll need to append the dataValues into your initial dictionary that you setup 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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