python - 将字典转换为方阵

标签 python numpy dictionary matrix

我想学习如何将字典转换为方阵。根据我所读到的内容,我可能需要将其转换为 numpy 数组,然后重新调整它的形状。我不想使用 reshape,因为我希望能够根据用户输入的信息来执行此操作。换句话说,我希望代码能够给出一个方阵,无论用户输入了多少个所有者和品种。

注意:该词典的所有者和品种因用户输入而异。用户可以输入 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 和 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}]]

关于python - 将字典转换为方阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39113110/

相关文章:

python - 我该如何解决这个无法导入错误: cannot import name in python3

python - heroku 限制和限制

收集满足给定条件的numpy数组元素的Pythonic方法

python - 如何为项目列表创建标签列表?

python - 垂直读取和写入多个字典到csv文件

java - ClassNotFound:apache.hadoop.io.ImmutableBytesWritable使用Python将Spark与Hbase集成

python - Pandas + HDF5面板数据存储可存储大数据

python - 使用 Numpy 和 Cython 加速距离矩阵计算

c++ - 为什么 insert_or_assign 没有迭代器重载?

c# - 防止在字典中重复查找相同元素