×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

How to use array with Python

How to use array with Python

How to use array with Python

(OP)
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

RE: How to use array with Python

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.

RE: How to use array with Python

(OP)
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

RE: How to use array with Python

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.

RE: How to use array with Python

(OP)

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

RE: How to use array with Python

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[i].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

RE: How to use array with Python

(OP)
vasekx,
thanks for the code I've learn some new advices (very usefull)!!! winky smile

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[i]

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources