1

This is a part of the code where you scrape the data from a website.I have scraped "a" tag and "span" tag from table tag and I have displayed the output of it which can be seen below.

for item in contents:
    data = [] # list for single table
    for tr in item.find_all('tr')[1:]: # find rows in table - skip row with headers `[1:]`
        a = tr.find('a')  # get only from first column
        a = a.text.strip()
        span = tr.find('span')  # get only from first column
        span = span.text.strip()
        data.append( (a, span) )
    print(data)
    tables.append(data)
for name, table in zip(names, tables):
    print('-', name)
    for a, span in table:
        print(a, span)

When you print "tables":

[[('GE', 'General Electric Co'), ('T', 'AT&T Inc'), ('BAC', 'Bank of America Corp'), ('AMD', 'Advanced Micro Devices Inc'), ('CMCSA', 'Comcast Corp'), ('M', "Macy's Inc"), ('PFE', 'Pfizer Inc'), ('F', 'Ford Motor Co'), ('WU', 'Western Union Co'), ('CSCO', 'Cisco Systems Inc')], [('BBY', 'Best Buy Co Inc'), ('UA', 'Under Armour Inc'), ('CMG', 'Chipotle Mexican Grill Inc'), ('UAA', 'Under Armour Inc'), ('ABMD', 'Abiomed Inc'), ('HRL', 'Hormel Foods Corp'), ('INTU', 'Intuit Inc'), ('DISCK', 'Discovery Inc'), ('DISCA', 'Discovery Inc'), ('CBRE', 'CBRE Group Inc')], [('DLTR', 'Dollar Tree Inc'), ('HPE', 'Hewlett Packard Enterprise Co'), ('XEC', 'Cimarex Energy Co'), ('HP', 'Helmerich and Payne Inc'), ('MCK', 'McKesson Corp'), ('IRM', 'Iron Mountain Inc'), ('CTL', 'Centurylink Inc'), ('COG', 'Cabot Oil & Gas Corp'), ('CAH', 'Cardinal Health Inc'), ('NTAP', 'NetApp Inc')]]

The actual output of the entire code is(when you print(a,span)):

- Most Actives
GE General Electric Co
T AT&T Inc
BAC Bank of America Corp
AMD Advanced Micro Devices Inc
CMCSA Comcast Corp
M Macy's Inc
PFE Pfizer Inc
F Ford Motor Co
WU Western Union Co
CSCO Cisco Systems Inc
- Gainers
BBY Best Buy Co Inc
UA Under Armour Inc
CMG Chipotle Mexican Grill Inc
UAA Under Armour Inc
ABMD Abiomed Inc
HRL Hormel Foods Corp
INTU Intuit Inc
DISCK Discovery Inc
DISCA Discovery Inc
CBRE CBRE Group Inc

I want to convert this to dictionary where if user enters "GE" ,the output will be "General Electric Co" ans so on.

4
  • Why are you printing a and span when you want to construct a dictionary? Just load the dictionary in a loop rather than printing the key, value pairs. Commented Nov 26, 2019 at 23:26
  • actually when i print a and span,this appears.this ouput i need to convert to dictionary of key,value pairs Commented Nov 26, 2019 at 23:27
  • Try printing table. Is it a dictionary already? Commented Nov 26, 2019 at 23:28
  • 2
    d = {k:v for k,v in zip(names, tables)} ? It is hard to know for sure without a minimal reproducible example. which includes names, tables and intended output. Commented Nov 26, 2019 at 23:28

3 Answers 3

3

Boom:

return_dict = {}

for name, table in zip(names, tables):
    print('-', name)
    for a, span in table:
        return_dict[a] = span

Would be nice to have a sample to test it on, but I assume this does what you want!

EDIT

Now that you have posted more of your code this would be a better answer:

return_dict = {}

for a, span in tables:
    return_dict[a] = span

or more concisely:

return_dict = {a:span for a, span in tables}

These are technically the same, the latter is just a comprehension of the former.

Sign up to request clarification or add additional context in comments.

Comments

3

Try this:

>>> dict(zip(names, map(dict, tables)))
{'Most Actives': {'GE': 'General Electric', 'T': 'AT&T Inc'}} # etc.

Comments

0

Just another way to answer the question.

tables = [[('GE', 'General Electric Co'), ('T', 'AT&T Inc'), ('BAC', 'Bank of America Corp'), ('AMD', 'Advanced Micro Devices Inc'), ('CMCSA', 'Comcast Corp'), ('M', "Macy's Inc"), ('PFE', 'Pfizer Inc'), ('F', 'Ford Motor Co'), ('WU', 'Western Union Co'), ('CSCO', 'Cisco Systems Inc')], [('BBY', 'Best Buy Co Inc'), ('UA', 'Under Armour Inc'), ('CMG', 'Chipotle Mexican Grill Inc'), ('UAA', 'Under Armour Inc'), ('ABMD', 'Abiomed Inc'), ('HRL', 'Hormel Foods Corp'), ('INTU', 'Intuit Inc'), ('DISCK', 'Discovery Inc'), ('DISCA', 'Discovery Inc'), ('CBRE', 'CBRE Group Inc')], [('DLTR', 'Dollar Tree Inc'), ('HPE', 'Hewlett Packard Enterprise Co'), ('XEC', 'Cimarex Energy Co'), ('HP', 'Helmerich and Payne Inc'), ('MCK', 'McKesson Corp'), ('IRM', 'Iron Mountain Inc'), ('CTL', 'Centurylink Inc'), ('COG', 'Cabot Oil & Gas Corp'), ('CAH', 'Cardinal Health Inc'), ('NTAP', 'NetApp Inc')]]
short = []
long = []
for x in range (len(tables)):
    for y in range (len(tables[x])):
        for z in range (len(tables[x][y])):
            if (z%2):
                long.append(tables[x][y][z])
            else:
                short.append(tables[x][y][z])
dictionary = dict(zip(short, long))

>>> dictionary["GE"]
'General Electric Co'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.