将字典转换为方形矩阵

时间:2016-08-24 01:34:07

标签: python numpy dictionary 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 has,因此我想弄清楚如何从字典中获取方阵。

      Bob      Sarah     Ann     Jen
Bob

Sarah

Ann

Jen

2 个答案:

答案 0 :(得分:3)

如果您能以

格式获取数据
{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}]]

答案 1 :(得分:0)

您可以通过生成嵌套字典来实现此目的,如:

{'Bob':   {'Bob': .., 'Sarah': .., 'Ann': .., 'Jen':..}
 'Sarah': {.. .. ..},
 'Ann':   {.. .. ..},
 'Jen':   {.. .. ..},
 }

以下是示例代码:

>>> my_dict = {'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}}
>>> new_dict = {}
>>> for key, value in my_dict.iteritems():
...     first_name, second_name = map(lambda x: x.strip(), key.split('VS'))
...     if first_name not in new_dict:
...         new_dict[first_name] = {}
...     new_dict[first_name][second_name] = value
... 
>>> new_dict
{'Sarah': {'Sarah': {'shepherd': 3, 'collie': 2, 'poodle': 2}, 
            'Ann': {'shepherd': 4, 'collie': 6, 'poodle': 3}, 
            'Jen': {'shepherd': 1, 'collie': 5, 'poodle': 8}, 
            'Bob': {'shepherd': 3, 'collie': 2, 'poodle': 4}
          }, 
 'Bob':   {'Sarah': {'shepherd': 1, 'collie': 5, 'poodle': 8}, 
           'Bob': {'shepherd': 3, 'collie': 2, 'poodle': 2}, 
           'Jen': {'shepherd': 3, 'collie': 2, 'poodle': 2}, 
           'Ann': {'shepherd': 3, 'collie': 2, 'poodle': 1}}, 
 'Jen':   {'Sarah': {'shepherd': 7, 'collie': 9, 'poodle': 2}, 
           'Bob': {'shepherd': 4, 'collie': 8, 'poodle': 1}, 
           'Jen': {'shepherd': 9, 'collie': 7, 'poodle': 2}, 
           'Ann': {'shepherd': 3, 'collie': 7, 'poodle': 2}
          }, 
 'Ann': {'Sarah': {'shepherd': 0, 'collie': 2, 'poodle': 4}, 
                   'Bob': {'shepherd': 6, 'collie': 2, 'poodle': 5}, 
                   'Jen': {'shepherd': 2, 'collie': 8, 'poodle': 2},                   
                   'Ann': {'shepherd': 13, 'collie': 2, 'poodle': 4}
        }
}