Dictionary keys and values to separate numpy arrays

后端 未结 6 571
迷失自我
迷失自我 2021-02-02 08:32

I have a dictionary as

Samples = {5.207403005022627: 0.69973543384229719, 6.8970222167794759: 0.080782939731898179, 7.8338517407140973: 0.10308033284258854, 8.53         


        
6条回答
  •  -上瘾入骨i
    2021-02-02 09:01

    You can use np.fromiter to directly create numpy arrays from the dictionary key and values views:

    In python 3:

    keys = np.fromiter(Samples.keys(), dtype=float)
    vals = np.fromiter(Samples.values(), dtype=float)
    

    In python 2:

    keys = np.fromiter(Samples.iterkeys(), dtype=float)
    vals = np.fromiter(Samples.itervalues(), dtype=float)
    

提交回复
热议问题