简体   繁体   中英

Tuple and numpy arrays in python

I have a list of Dictionaries, each consisting of arrays of strings and floats such as below:

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. 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

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. Expall = Products[*]['Exp']

Most of the time, I even have to store the data first and I use pickle in Python to do that. 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.

Following could work ( List comprehension ):

if you have separated Product i,

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

if you have Products ,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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