如何将一个字典转换为玲阶矩阵_将字典转换为方矩阵

I am wanting to learn how to convert a dictionary into a square matrix. From what I have read, I may need to convert this into a numpy array and then reshape it. I do not want to use reshape as I want to be able to do this based on information a user puts in. In other words, I want a code to give out a square matrix no matter how many owners and breeds are input by the user.

Note: The owners and breeds for this dictionary vary upon user input. A user can input 100 names and 50 breeds, or they can input 4 names and 5 breeds. In this example, I did four names and three dogs.

dict1 =

{'Bob VS Sarah': {'shepherd': 1,'collie': 5,'poodle': 8},

'Bob VS Ann': {'shepherd': 3,'collie': 2,'poodle': 1},

'Bob VS Jen': {'shepherd': 3,'collie': 2,'poodle': 2},

'Sarah VS Bob': {'shepherd': 3,'collie': 2,'poodle': 4},

'Sarah VS Ann': {'shepherd': 4,'collie': 6,'poodle': 3},

'Sarah VS Jen': {'shepherd': 1,'collie': 5,'poodle': 8},

'Jen VS Bob': {'shepherd': 4,'collie': 8,'poodle': 1},

'Jen VS Sarah': {'shepherd': 7,'collie': 9,'poodle': 2},

'Jen VS Ann': {'shepherd': 3,'collie': 7,'poodle': 2},

'Ann VS Bob': {'shepherd': 6,'collie': 2,'poodle': 5},

'Ann VS Sarah': {'shepherd': 0,'collie': 2,'poodle': 4},

'Ann VS Jen': {'shepherd': 2,'collie': 8,'poodle': 2},

'Bob VS Bob': {'shepherd': 3,'collie': 2,'poodle': 2},

'Sarah VS Sarah': {'shepherd': 3,'collie': 2,'poodle': 2},

'Ann VS Ann': {'shepherd': 13,'collie': 2,'poodle': 4},

'Jen VS Jen': {'shepherd': 9,'collie': 7,'poodle': 2}}

For example, I want a 4 x 4 matrix (again, the user can input any number of dog breeds so 3 breeds is not a restriction), since there are four owners.

I apologize ahead of time for not putting in what I want the end result to look like and usually I do. I am just proud of myself for making dict1 :). So the dictionary should be in a form similar to below, but I am not sure how to incorporate the different breeds. The hard part for me is that I am only needing one matrix. I also plan on using the matrix solver numpy has, hence why I am wanting to figure out how to get a square matrix from a dictionary.

Bob Sarah Ann Jen

Bob

Sarah

Ann

Jen

解决方案

If you can get your data in the format

{name1: {name1:data, name2:data, name3:data, ...},

name2: {name1:data, name2:data, name3:data, ...},

...

}

then you can just hand it to a pandas DataFrame and it will make it for you. The data at position row = name1 and col = name2 will be the value of name1 vs name2. Here is the code that will do it:

from collections import defaultdict

import pandas

result = defaultdict(dict)

for key,value in dict1.items():

names = key.split()

name1 = names[0]

name2 = names[2]

result[name1][name2] = value

df = pandas.DataFrame(result).transpose()

print(df)

This gives the following output:

Ann Bob Jen Sarah

Ann {'shepherd': 13, 'collie': 2, 'poodle': 4} {'shepherd': 6, 'collie': 2, 'poodle': 5} {'shepherd': 2, 'collie': 8, 'poodle': 2} {'shepherd': 0, 'collie': 2, 'poodle': 4}

Bob {'shepherd': 3, 'collie': 2, 'poodle': 1} {'shepherd': 3, 'collie': 2, 'poodle': 2} {'shepherd': 3, 'collie': 2, 'poodle': 2} {'shepherd': 1, 'collie': 5, 'poodle': 8}

Jen {'shepherd': 3, 'collie': 7, 'poodle': 2} {'shepherd': 4, 'collie': 8, 'poodle': 1} {'shepherd': 9, 'collie': 7, 'poodle': 2} {'shepherd': 7, 'collie': 9, 'poodle': 2}

Sarah {'shepherd': 4, 'collie': 6, 'poodle': 3} {'shepherd': 3, 'collie': 2, 'poodle': 4} {'shepherd': 1, 'collie': 5, 'poodle': 8} {'shepherd': 3, 'collie': 2, 'poodle': 2}

A simple conversion to a numpy array would look like:

numpy_array = df.as_matrix()

print(numpy_array)

[[{'shepherd': 13, 'collie': 2, 'poodle': 4}

{'shepherd': 6, 'collie': 2, 'poodle': 5}

{'shepherd': 2, 'collie': 8, 'poodle': 2}

{'shepherd': 0, 'collie': 2, 'poodle': 4}]

[{'shepherd': 3, 'collie': 2, 'poodle': 1}

{'shepherd': 3, 'collie': 2, 'poodle': 2}

{'shepherd': 3, 'collie': 2, 'poodle': 2}

{'shepherd': 1, 'collie': 5, 'poodle': 8}]

[{'shepherd': 3, 'collie': 7, 'poodle': 2}

{'shepherd': 4, 'collie': 8, 'poodle': 1}

{'shepherd': 9, 'collie': 7, 'poodle': 2}

{'shepherd': 7, 'collie': 9, 'poodle': 2}]

[{'shepherd': 4, 'collie': 6, 'poodle': 3}

{'shepherd': 3, 'collie': 2, 'poodle': 4}

{'shepherd': 1, 'collie': 5, 'poodle': 8}

{'shepherd': 3, 'collie': 2, 'poodle': 2}]]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值