Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7968943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:09:26+00:00 2026-06-04T07:09:26+00:00

I have a huge dictionary something like this: d[id1][id2] = value example: books[auth1][humor] =

  • 0

I have a huge dictionary something like this:

d[id1][id2] = value

example:

books["auth1"]["humor"] = 20
books["auth1"]["action"] = 30
books["auth2"]["comedy"] = 20

and so on..

Each of the “auth” keys can have any set of “genres” associated wtih them. The value for a keyed item is the number of books they wrote.

Now what I want is to convert it in a form of matrix…something like:

                    "humor"       "action"        "comedy"
      "auth1"         20            30               0
      "auth2"          0            0                20

How do i do this?
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:09:27+00:00Added an answer on June 4, 2026 at 7:09 am

    Use a list comprehension to turn a dict into a list of lists and/or a numpy array:

    np.array([[books[author][genre] for genre in sorted(books[author])] for author in sorted(books)])
    

    EDIT

    Apparently you have an irregular number of keys in each sub-dictionary. Make a list of all the genres:

    genres = ['humor', 'action', 'comedy']
    

    And then iterate over the dictionaries in the normal manner:

    list_of_lists = []
    for author_name, author in sorted(books.items()):
        titles = []
        for genre in genres:
            try:
                titles.append(author[genre])
            except KeyError:
                titles.append(0)
        list_of_lists.append(titles)
    
    books_array = numpy.array(list_of_lists)
    

    Basically I’m attempting to append a value from each key in genres to a list. If the key is not there, it throws an error. I catch the error, and append a 0 to the list instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a huge dictionary of blank values in a variable called current like
I have this method with a huge switch statement like this: public bool ExecuteCommand(string
I have a huge dictionary (like a database) where I want to find some
I have a csv file which is something like this: book_store_id book_price name 1
I have a python module that makes use of a huge dictionary global variable,
So I have this huge XML file that I am converting to an object
Let's say I have a dictionary of names (a huge CSV file). I want
I have an array like this - string[] input = new string[] {bRad, Charles,
I have huge of logs like: Apr 15 06:24:52 11.250.30.X:53516 [15/Apr/2012:06:24:51.504] userA 200 GET
I have huge table with 1 million records, i would like to modify some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.