Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IDS on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use array with Python

Status
Not open for further replies.

barney75

Mechanical
Jan 22, 2007
58
I'm trying to store some data with Python into a two dimensional array and I have some problems with the use of the List object indexes. Let consider for exaple the following list a=[[1,2,3],[4,5,6],[7,8,9]]. Ok, after this command, if you type a[0][0] the result is 1, if you type a[0][:] the result is [1, 2, 3], but if you want all the elements of the first column (i.e. 1, 4, and 7) and you type a[:][0] the result is [1, 2, 3]!!! What is wrong or in other words what should I type to print the elements of the first column?

Thanks for the help

bye
 
Replies continue below

Recommended for you

a[:] represents a (hard) copy of a and it has the same content as a. Therefore a[:][0] is the first object in a[:] which is the same as the first object in a , namely the list [1,2,3]. (a[i:j] represents slice of a from i to j )

I suggest you read the documentation on List data type in the standard Python documentation (e.g., Python Library Reference Chapter 2. Built-In Objects 2.3.6 Sequence Types ).

For your specific problem, you should try this:
first_column=[v[0] for v in a]
print first_column

Also, you should not mix the concepts of list and array which are different.
 
Xerf thanks for your help.
I know that list and array in Python have different meaning, I use the term "array" only to give the right idea from a mathematical point of view. I'd like to ask another advice, that is, which kind of Python structure would you use if you had to store for example the Sectional Moment (SM1) of a set of beams elements (pipe) through all the analysis steps and frames and then save them into a text file? Please let me know if is it clear. I'm a fresh user of Python and I really appreciate any suggestion!

Thanks
 
Python is a very flexible language. Data abstraction and storage can be cast in very different forms:
You could use a list of lists, a dictionary of lists, a dictionary of objects etc.

If your question relates to ABAQUS Scripting Interface then maybe it is not necessary to store the data since that is already stored in the .odb file. Then you only have to get access to a specific FieldOutput object from .odb file (containing SM1) and iterate over the values it stores. If this is the case I strongly recommend you to spend some time reading (browsing) ABAQUS Scripting User's Manual. This manual contains a good introduction to Python and to ABAQUS Scripting Interface and explain the architecture of data structures used by ABAQUS.

 

I'd like to extract data from odb and write them in txt file because then I can make graphs on excel, make some calculation and so on, in other words I postprocess the data out of Abaqus and therefore out of odb. I've already accessed to the FieldOutput object but I'm trying to write a postprocessing python program that allows me to manage the huge amount of data that I'm extracting from odb. I'm reading the Abaqus Manual and I'm practising with Python.

Xerf thanks a lot
 
barney75: I use the following script to save eigenfrequencies in one of my scripts (I just use a list, not an array). Hope, it should be helpful (sometimes hunting for the right information is really time consuming, when there is also a lot of ballast in the documentation ;o) ). For your purpose, you would not use frames to export the values, but the FieldOutput, as xerf has mentioned (it could cost just a change of one line).

def saveData(dbName, stpName, fileName, calcV):
oOdb=openOdb(path=dbName) # opens database with specified name
oFrames=oOdb.steps[stpName].frames # defines object with all values stored with each frame
frVal = [] # defines a list variable

i=0
for value in oFrames:
frVal.append(oFrames.frequency) # appends eigenfrequency value to the list variable
i=i+1

theFile=open(fileName + '_' + str(calcV) + '.txt','w') # creates file object - opens file for writing (if 'a' at the end, opens file for append)
theFile.write('Database: ' + dbName + '\n')
theFile.write('Step: ' + stpName + '\n\n')

i=0
for data in frVal:
if str(data) != '[]': # I have this value sometimes as the first value in the list and I really don't want to save it in a file ;o)
theFile.write('Freq ' + str(i) + ' ')
theFile.write(str(data))
i=i+1
theFile.write('\n')

theFile.close() # closes the file object
return frVal # returns the values in the list back to script, so I can use it for other purposes after saving data into a file
 
vasekx,
thanks for the code I've learn some new advices (very usefull)!!! ;-)

One more question, have you ever used elbow element? For this element you can choose the number of the section point in the circumferential direction but also through the thickness. In order to extract the parameter of interest I tried with:

odb.steps[step].frames[-1].fieldOutputs['S'].locations[0].sectionPoints

the problem is that if you have, let say, 20 point in the circumference and 5 in the radial, i ranges from 0 to 99.
How can I find the number of the section point in the two direction? Clearly I already know it but I need it in order to print it on the screen and then I'll choose the specific point through the command raw_input.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor