Home > OS >  How to create a dictionary from a list of tuples without overwriting the keys
How to create a dictionary from a list of tuples without overwriting the keys

Time:10-02

I have a script that extracts files from a website, the key across the files are the same.

I want to create a dictionary from these files extracted files without overwriting the keys.

table_information = []
for t in my_url[:5]:
    page_detail = requests.get(t)
    tree_1 = html.fromstring(page_detail.content)
    title = ''.join(tree_1.xpath('//div/h1[@class="title"]/text()'))
    Track = title.split(' ')[0].strip()
    date = title.split('-')[1].strip()
    year = pd.to_datetime('now').year 
    race_date =  pd.to_datetime(date   ' '   str(year)).strftime('%d/%m/%Y')    
    table_information.append((race_date,Track))
    

The above code extracts the files and appends them to a list.

The extracted files are like this.

table_information = 
[('Angle', '01/10/2021', ['342m', '342m', '530m', '342m', '342m', '595m', '530m', '342m', '342m', '342m']), 
('Ballarat', '02/10/2021', ['390m', '390m', '390m', '390m', '450m', '450m', '450m', '450m', '390m', '390m']), 
('Bendigo', '02/10/2021', ['425m', '425m', '425m', '425m', '500m', '500m', '500m', '425m', '425m', '425m', '425m', '425m'])]

Now I want to a dictionary of this form.

    new_dict = { 'Date':['01/10/2021', '02/10/2021', '02/10/2021'],
                'Track': ['Angle', 'Ballarat', 'Bendigo'],
                 'Distance': [['342m', '342m', '530m', '342m', '342m', '595m', '530m', '342m', '342m', '342m'],
                           ['390m', '390m', '390m', '390m', '450m', '450m', '450m', '450m', '390m', '390m'],
                           ['425m', '425m', '425m', '425m', '500m', '500m', '500m', '425m', '425m', '425m', '425m', '425m']]
                
}

I tried to create the dictionary using the below code, but I keys repeated multiple times. Please how can I achieve this?.

greyhound = {}
new_dict = {}
greyhoud['Date'] = [race_date]
greyhoud['Distatnce'] = [Distatnce]
greyhoud['Track'] = [Track]
for k,v in greyhoud.items():
    for key in k:
        if key in new_dict:
            new_dict[k].append(v)
        else:
            new_dict[k] = [v]

CodePudding user response:

Just loop through table_information, appending each element in the tuples to the appropriate dictionary elements.

greyhound = {'Date': [], 'Track': [], 'Distance': []}

for track, date, distance in table_information:
    greyhound['Track'].append(track)
    greyhound['Date'].append(date)
    greyhound['Distance'].append(distance)

CodePudding user response:

Just loop through the list and append them:

table_information = [
    ('Angle', '01/10/2021', ['342m', '342m', '530m', '342m', '342m', '595m', '530m', '342m', '342m', '342m']), 
    ('Ballarat', '02/10/2021', ['390m', '390m', '390m', '390m', '450m', '450m', '450m', '450m', '390m', '390m']), 
    ('Bendigo', '02/10/2021', ['425m', '425m', '425m', '425m', '500m', '500m', '500m', '425m', '425m', '425m', '425m', '425m'])
]

new_dict = {
    'Date': [],
    'Track': [],
    'Distance': []
}

for track, date, distance in table_information:
    new_dict['Track'].append(track)
    new_dict['Date'].append(date)
    new_dict['Distance'].append(distance)
  • Related