简体   繁体   English

将字典列表转换为 numpy 数组

[英]convert list of dictionaries to numpy array

I have a list of dictionaries that is as follows:我有一个字典列表,如下所示:

{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044}
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044}
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044}
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044}
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044}
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044}
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044}
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044}
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044}
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044}
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044}
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044}
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044}

I'm trying to convert it to a numpy array with two columns for just the 'x' and 'z' values.我正在尝试将其转换为 numpy 数组,其中两列仅用于“x”和“z”值。

Is there a simple way to do this?有没有一种简单的方法可以做到这一点?

data = [{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044},
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044},
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044},
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044},
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044},
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044},
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044},
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044},
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044},
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044},
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044},
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044},
{'x': -0.002222188049927354, 'y': 0.014999999664723873, 'z': -0.45333319902420044}]

pd.DataFrame(data)[['x', 'z']].values

array([[-0.00222219, -0.4533332 ],
       [-0.00222219, -0.4533332 ],
       [-0.00222219, -0.4533332 ],
       [-0.00222219, -0.4533332 ],
       [-0.00222219, -0.4533332 ],
       [-0.00222219, -0.4533332 ],
       [-0.00222219, -0.4533332 ],
       [-0.00222219, -0.4533332 ],
       [-0.00222219, -0.4533332 ],
       [-0.00222219, -0.4533332 ],
       [-0.00222219, -0.4533332 ],
       [-0.00222219, -0.4533332 ],
       [-0.00222219, -0.4533332 ]])

Like so:像这样:

thearray = pd.DataFrame(dictlist)[['x', 'z']].values

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM