将2d字典转换为numpy矩阵 [英] converting a 2d dictionary to a numpy matrix

查看:167
本文介绍了将2d字典转换为numpy矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一本巨大的字典,像这样:

I have a huge dictionary something like this:

d[id1][id2] = value

示例:

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

以此类推.

每个"auth"键都可以具有与之关联的任何类型"集.密钥项目的价值是他们写的书的数量.

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

我该怎么做? 谢谢

推荐答案

使用列表推导将字典转换为列表列表和/或numpy数组:

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

编辑

显然,每个子词典中的键数都是不规则的.列出所有流派:

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)

基本上,我试图将genres中每个键的值附加到列表中.如果密钥不存在,则会引发错误.我发现了错误,然后将0附加到列表中.

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.

这篇关于将2d字典转换为numpy矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆