Newbie: changing an attribute of objects in a list

Ben Simuyandi bsimuyandi at
Wed Jun 11 13:09:41 CEST 2003

Hello. I am very new to Python and I'm stuck with a problem. I am using
Zope, and the problem is:

I have a list of objects, each of which has several attributes. I would like
to sort these objects by a specific attribute, which is a string. This I can
manage OK. But if two objects have the same attribute value, I would like to
change the older (age determined by another attribute) object's attribute
to a new value of (old value+1), then go through the list and see if any
other object has this value, if so give it (old value+2) etc.

I hope I have made the problem clear. I know that my code is wrong, but I
would like some hints as to what I am doing wrong. Some of it comes down to
a lack of understanding of Python.

The ordering attribute I'll call "ordering", the age attribute I'll call
"age", and the list of objects is "contents". I thought that the way to do
this was by calling a function recursively. Anyway, below is the code, any
feedback is appreciated.

def check_listingorder(x, contents):
  for y in contents:
    if x.Id != y.Id:                                           #each object
has a unique id
      xorder=int(x.ordering)                             #convert the string
to an integer
      if xorder==yorder:                                    #if the ordering
attribute is the same then...
        if x.age> y.age:                                       #if x is
older than y then...
         <set object ordering value to xorder>
          check_listingorder(x, contents)
#call function again?
        elif y.age> x.age:                                       #if y is
older than x then...
         <set object ordering value to yorder>
          check_listingorder(y, contents)                              #call
function again?
  return contents                                                #return the

for x in contents:                                               #originally
called with:
  check_listingorder(x, contents)

More information about the Python-list mailing list