0

I am writing a script in python that is accessing a result database from an engineering software. It consists of several objects, each consisting of ID, type, results etc.

They are all accessed in the following manner:

object.values[i].ID
object.values[i].result
etc.

The question is, is it possible to extract the ID and result and put them into two lists in a more efficient way than to loop through the entire object with a for-loop.

Is this a standardized object of some sort?

Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
  • 1
    Hard to tell what would be better for you. You're not giving us much to go on. What did you try already that we could help you improve upon? Also welcome to Stackoverflow fellow Swede! (i'm assuming) – Henrik Andersson Apr 24 '13 at 12:27
  • http://stackoverflow.com/questions/1251692/how-to-enumerate-an-objects-properties-in-python – Maria Ines Parnisari Apr 24 '13 at 12:29
  • Hi and thanks! I can't really supply you with much information since the data structure is unclear to me. What I can see is that a dict that consists of several entries of which one is called "values" which is a FieldValueArray object. Which I assume is software specific. Currently I am forced to run a for loop in which I extract ID, result etc and append to lists. I would like to be able to extract the list by writing object.values.ID instead of specifying object.values[i].ID but perhaps this object is unique for this software and therefor hard for you to comment on. – Johan Cederlund Apr 24 '13 at 12:58

1 Answers1

0

If the goal is to extract both attributes in a single list, you could use a list comprehension:

l = [[item.ID, item.result] for item in object.values]

If you need them in two separate lists, either use a hand-crafted loop, or use this on the previously computed result:

ids, results = zip(*l)
icecrime
  • 74,451
  • 13
  • 99
  • 111