【问题标题】:Turning the values of a dictionary into a vector将字典的值转换为向量
【发布时间】:2021-07-14 23:59:44
【问题描述】:

我正在尝试将字典的数值转换为向量,因为值的顺序很重要。我以前做过类似的事情,但由于某种原因它在这里不起作用,我们将不胜感激。

g_dict = dict()
g_dict['E*S'] = -1.502
g_dict['S']   = -1.720
g_dict['E']      = 0.802
g_dict['I*E']  = -2.444
g_dict['P'] = -2.377
g_dict['I']   = -0.389
g_dict['I*E*S']  = 0.703


for spc in species:
    g_vec[species.index(spc)] = g_dict[spc]

它告诉我,如果有帮助,浮动不能接受这个项目分配。

【问题讨论】:

    标签: python arrays dictionary


    【解决方案1】:

    使用list comprehension:

    g_vec = [g_dict[s] for s in species]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-17
      • 2019-09-13
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      相关资源
      最近更新 更多