Seeking advice from seasoned Python programmer
|
10-19-2017, 06:53 PM
(This post was last modified: 10-19-2017 08:01 PM by Namir.)
Post: #1
|
|||
|
|||
Seeking advice from seasoned Python programmer
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 |
|||
10-19-2017, 10:29 PM
Post: #2
|
|||
|
|||
RE: Seeking advice from seasoned Python programmer
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. |
|||
10-20-2017, 02:29 AM
Post: #3
|
|||
|
|||
RE: Seeking advice from seasoned Python programmer
(10-19-2017 10:29 PM)emece67 Wrote: Not used to such class based constructions, but if: 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 |
|||
10-20-2017, 07:56 AM
(This post was last modified: 10-20-2017 08:16 AM by emece67.)
Post: #4
|
|||
|
|||
RE: Seeking advice from seasoned Python programmer
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. |
|||
10-20-2017, 08:08 AM
Post: #5
|
|||
|
|||
RE: Seeking advice from seasoned Python programmer
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/3357...-container https://docs.python.org/3/library/collec...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. Wikis are great, Contribute :) |
|||
10-20-2017, 10:35 AM
Post: #6
|
|||
|
|||
RE: Seeking advice from seasoned Python programmer
(10-20-2017 07:56 AM)emece67 Wrote: Happy to know it was helping. 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!!! |
|||
10-20-2017, 05:29 PM
Post: #7
|
|||
|
|||
RE: Seeking advice from seasoned Python programmer
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. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 2 Guest(s)