简体   繁体   English

python 中的元组和 numpy arrays

[英]Tuple and numpy arrays in python

I have a list of Dictionaries, each consisting of arrays of strings and floats such as below:我有一个字典列表,每个字典由 arrays 字符串和浮点数组成,如下所示:

Product1 = {
 'Name': 'TLSK',
 'Name2': 'B1940',
 'Tagid': '23456222',
 'Cord': np.array(['09:42:23', '9', '-55:52:32',  '9']),
 'Cord2': np.array([432.34, 222.115]),
 'Ref': 'Siegman corp. 234',
 'Exp': 22.0,
 'CX': np.array([0.00430, 0.00069, 0.00094])
}

I sometimes need access to certain elements of Dictionary for further calculations.我有时需要访问 Dictionary 的某些元素以进行进一步计算。 What I do is I first merge them as follows:我要做的是首先将它们合并如下:

Products = (Product1, Product2, Product3, ....)

Then I use for loop to store a certain element of each Dictionary as follows然后我使用 for 循环来存储每个 Dictionary 的某个元素,如下所示

Expall=[]
for i in Products:
    exp = i['Exp']
    Expall.append(exp)

To me this seems like an inefficient/bad coding and I was wondering if there is a better way to do it.对我来说,这似乎是一种低效/糟糕的编码,我想知道是否有更好的方法来做到这一点。 I am coming from IDL language and for instance, in IDL you could have access to that info without a for loop like this.我来自 IDL 语言,例如,在 IDL 中,您可以在没有这样的for loop的情况下访问该信息。 Expall = Products[*]['Exp']

Most of the time, I even have to store the data first and I use pickle in Python to do that.大多数时候,我什至必须先存储数据,然后我pickle in Python来做到这一点。 Since I am a bit new to python and I heard few good things about pandas and etc, I wanted to see if there is a more efficient/quicker way to handle all this stuff.由于我对 python 有点陌生,而且我听说过关于 pandas 等的好消息,我想看看是否有更有效/更快的方法来处理所有这些事情。

Following could work ( List comprehension ):以下可以工作(列表理解):

if you have separated Product i,如果您已分离产品i,

[product['Exp'] for product in [Product1, Product2]]

if you have Products ,如果您有产品

[product['Exp'] for product in Products]

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

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