Skip to main content
 首页 » 编程设计

python之从字典创建数组

2024年10月24日12bluestorm

我在字典中有一些词,根据这些词和一些句子,我想创建一个特定的数组。

words = {'a': array([ 1.78505888, -0.40040435, -0.2555062 ]), 'c': array([ 0.58101204, -0.23254054, -0.5700197 ]), 'b': array([ 1.17213122,  0.38232652, -0.78477569]), 'd': array([-0.07545012, -0.10094538, -0.98136142])} 
 
sentences = [['a', 'c'], ['b', 'a', 'd'], ['d', 'c']] 

我想要的是得到一个数组,第一行是垂直堆叠的'a'和'c'的值。
第二行是垂直堆叠的 'b' 和 'a' 的值。
第三,'d' 和 'c' 的值垂直堆叠。

我试过这个:

np.vstack((words[word] for word in sentences[0])) 
>>> array([[ 1.78505888, -0.40040435, -0.2555062 ], 
   [ 0.58101204, -0.23254054, -0.5700197 ]]) 

所以这是我的第一行,但我无法使用列表理解(仅针对一个)对“句子”执行此操作。

编辑: 基本上我想做的是以下内容

first_row = np.vstack((words[word] for word in sentences[0])) 
second_row = np.vstack((words[word] for word in sentences[1])) 
third_row = np.vstack((words[word] for word in sentences[2])) 
 
l = [] 
l.append(first_row) 
l.append(second_row) 
l.append(third_row) 
 
print np.array(l) 
>>> [[[ 1.78505888 -0.40040435 -0.2555062 ] 
      [ 0.58101204 -0.23254054 -0.5700197 ]] 
 
     [[ 1.17213122  0.38232652 -0.78477569] 
      [ 1.78505888 -0.40040435 -0.2555062 ] 
      [-0.07545012, -0.10094538, -0.98136142]] 
 
     [[-0.07545012 -0.10094538 -0.98136142] 
      [ 0.58101204 -0.23254054 -0.5700197 ]]] 

请您参考如下方法:

您可以使用 np.searchsorted建立words的字符串键和sentences的每个元素中的字符串之间的对应关系。对 sentences 中的所有元素重复此过程以获得最终结果。因此,我们将只有一层循环来解决它。实现看起来像这样 -

K = words.keys() 
sortidx = np.argsort(K) 
V = np.vstack(words.values())[sortidx] 
out = [V[np.searchsorted(K,S,sorter=sortidx)] for S in sentences] 

sample 运行-

In [122]: words 
Out[122]:  
{'a': array([ 1.78505888, -0.40040435, -0.2555062 ]), 
 'b': array([ 1.17213122,  0.38232652, -0.78477569]), 
 'c': array([ 0.58101204, -0.23254054, -0.5700197 ]), 
 'd': array([-0.07545012, -0.10094538, -0.98136142])} 
 
In [123]: sentences 
Out[123]: [['a', 'c'], ['b', 'a', 'd'], ['d', 'c']] 
 
In [124]: K = words.keys() 
     ...: sortidx = np.argsort(K) 
     ...: V = np.vstack(words.values())[sortidx] 
     ...: out = [V[np.searchsorted(K,S,sorter=sortidx)] for S in sentences] 
     ...:  
 
In [125]: out 
Out[125]:  
[array([[ 1.78505888, -0.40040435, -0.2555062 ], 
        [ 0.58101204, -0.23254054, -0.5700197 ]]), 
 array([[ 1.17213122,  0.38232652, -0.78477569], 
        [ 1.78505888, -0.40040435, -0.2555062 ], 
        [-0.07545012, -0.10094538, -0.98136142]]), 
 array([[-0.07545012, -0.10094538, -0.98136142], 
        [ 0.58101204, -0.23254054, -0.5700197 ]])]