HP Forums
Seeking advice from seasoned Python programmer - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: Not HP Calculators (/forum-7.html)
+--- Forum: Not remotely HP Calculators (/forum-9.html)
+--- Thread: Seeking advice from seasoned Python programmer (/thread-9329.html)



Seeking advice from seasoned Python programmer - Namir - 10-19-2017 06:53 PM

Hi,

I am learning to program in Python (and also in numpy, scipy, etc.). I would like to learn how to work with arrays of structures in Python. The structure I have in mind has the following fields:

1) An array of float numbers
2) A scalar float value.
3) A scalar float value.

I have tried to use a Python class to define the above structure, and then build an array of class instances (call the array swam). However when I use something like swarm[i].position[k] (where position is the first field which is an array) Python complains of to many indices. I have taken care of creating the first field as a numpy array before I attempt to access it.

I have searched the Internet and found examples of creating arrays of classes (used to define structures) that contain data members (i.e. fields of structures) that are scalar only!!

Any help is appreciated. I can even send you the listing.

Thanks!

Namir


RE: Seeking advice from seasoned Python programmer - emece67 - 10-19-2017 10:29 PM

Not used to such class based constructions, but if:

a = [[1, 2, 3], 4, 5] # your structure
b = [[6, 7, 8], 9, 10] # another one
c = [a, b] # an array containing two of them

then c[1] returns [[6, 7, 8], 9, 10], which is b
c[1][0] returns [6, 7, 8] which is the array in b
c[1][0][2] returns 8, which is the last member of the array inside b.

Regards.


RE: Seeking advice from seasoned Python programmer - Namir - 10-20-2017 02:29 AM

(10-19-2017 10:29 PM)emece67 Wrote:  Not used to such class based constructions, but if:

a = [[1, 2, 3], 4, 5] # your structure
b = [[6, 7, 8], 9, 10] # another one
c = [a, b] # an array containing two of them

then c[1] returns [[6, 7, 8], 9, 10], which is b
c[1][0] returns [6, 7, 8] which is the array in b
c[1][0][2] returns 8, which is the last member of the array inside b.

Regards.

Thank you so much for your hint. You have guides a blind man (me)!! I recoded the program, removed the class definition, avoided using numpy's array, and built the array of structures brick by brick, using nested loops. I used the double and triple indices with the array of structures, as you suggested, and IT WORKED!!!!!!!!!!!!!!!!!!!!

Thanks again!!!

Namir


RE: Seeking advice from seasoned Python programmer - emece67 - 10-20-2017 07:56 AM

Happy to know it was helping.

You can also use a dictionary to store your structure, as:

a = {'array':[1, 2, 3], 'scalar1':4, 'scalar2':5} # your structure
b = {'scalar2':10, 'scalar1':9, 'array':[6, 7, 8]} # another one, just to remark that order is unimportant now
c = [a, b] # an array containing two of them

then c[1] returns {'array': [6, 7, 8], 'scalar1': 9, 'scalar2': 10}, which is b
c[1]['array'] returns [6, 7, 8] which is the array in b
c[1]['array'][2] returns 8, which is the last member of the array inside b.
c[1]['scalar1'] returns 9, which is the 1st scalar in b.

Perhaps more legible. Regards.


RE: Seeking advice from seasoned Python programmer - pier4r - 10-20-2017 08:08 AM

Did not touch python for a while. Actually arrays of arrays, or dictionaries like emece67 said should be pretty good.

Maybe this can help further with named tuples:
https://stackoverflow.com/questions/3357581/using-python-class-as-a-data-container

https://docs.python.org/3/library/collections.html#collections.namedtuple

Surely python has a lot of ways to solve a problem. I cannot bring myself to like the forced indentation though. I mean, it is a wonderful too, but if there would be a python with blocks delimited not only by blank lines, I would pick that.


RE: Seeking advice from seasoned Python programmer - Namir - 10-20-2017 10:35 AM

(10-20-2017 07:56 AM)emece67 Wrote:  Happy to know it was helping.

You can also use a dictionary to store your structure, as:

a = {'array':[1, 2, 3], 'scalar1':4, 'scalar2':5} # your structure
b = {'scalar2':10, 'scalar1':9, 'array':[6, 7, 8]} # another one, just to remark that order is unimportant now
c = [a, b] # an array containing two of them

then c[1] returns {'array': [6, 7, 8], 'scalar1': 9, 'scalar2': 10}, which is b
c[1]['array'] returns [6, 7, 8] which is the array in b
c[1]['array'][2] returns 8, which is the last member of the array inside b.
c[1]['scalar1'] returns 9, which is the 1st scalar in b.

Perhaps more legible. Regards.

Thanks! Your solution ALSO WORKED. It was easy to go from the first solution to this one since I was using variables to select the proper indices for the swarm array of structures. All I had to do is search and replace these variable names and enclose them in single quote. Program works like a charm!

Thanks!!!


RE: Seeking advice from seasoned Python programmer - emece67 - 10-20-2017 05:29 PM

Namir,

Although the numpy/scipy packages are powerful, they may be a little complex for casual users or beginners. Take a look at the mpmath package, much simpler but really powerful.

Regards.