将字典转换为方矩阵 [英] Converting a dictionary into a square matrix
问题描述
我想学习如何将字典转换为方矩阵.从我的阅读中,我可能需要将其转换为一个numpy数组,然后对其进行整形.我不想使用重塑形状,因为我希望能够基于用户输入的信息来执行此操作.换句话说,无论用户输入多少所有者和品种,我都希望代码给出方矩阵.
注意:该词典的所有者和品种随用户输入而变化.用户可以输入100个名称和50个品种,也可以输入4个名称和5个品种.在这个例子中,我做了四个名字和三只狗.
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}}
例如,我想要一个4 x 4的矩阵(同样,用户可以输入任意数量的狗品种,因此3个品种不是一个限制),因为有四个所有者.
我为未按要求输入最终结果而道歉,通常我会这样做.我为自己做出dict1 :)感到骄傲.因此,该词典的格式应类似于以下内容,但我不确定如何合并不同的品种.对我而言,最困难的部分是我只需要一个矩阵.我还计划使用numpy具有的矩阵求解器,因此为什么我想弄清楚如何从字典中获取方矩阵.
Bob Sarah Ann Jen
Bob
Sarah
Ann
Jen
如果您可以采用
格式的数据{name1: {name1:data, name2:data, name3:data, ...},
name2: {name1:data, name2:data, name3:data, ...},
...
}
然后您可以将其交给pandas DataFrame,它将为您制作.位置row = name1 and col = name2
上的数据将是name1 vs name2
的值.这是执行此操作的代码:
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)
这将提供以下输出:
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}
向numpy数组的简单转换如下所示:
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}]]
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}]]
这篇关于将字典转换为方矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!