Dictionary keys and values to separate numpy arrays

后端 未结 6 581
迷失自我
迷失自我 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条回答
  •  悲哀的现实
    2021-02-02 08:52

    Just assign all of the values to a list, and then convert to a np.array().

    import numpy as np
    
    Samples = {5.207403005022627: 0.69973543384229719, 6.8970222167794759: 0.080782939731898179, 7.8338517407140973: 0.10308033284258854, 8.5301143255505334: 0.018640838362318335, 10.418899728838058: 0.14427355015329846, 5.3983946820220501: 0.51319796560976771}
    
    keys = np.array(Samples.keys())
    vals = np.array(Samples.values())
    

    Or, if you want to iterate over it:

    import numpy as np
    
    Samples = {5.207403005022627: 0.69973543384229719, 6.8970222167794759: 0.080782939731898179, 7.8338517407140973: 0.10308033284258854, 8.5301143255505334: 0.018640838362318335, 10.418899728838058: 0.14427355015329846, 5.3983946820220501: 0.51319796560976771}
    
    keys = vals = []
    
    for k, v in Samples.items():
        keys.append(k)
        vals.append(v)
    
    keys = np.array(keys)
    vals = np.array(vals)
    

提交回复
热议问题