Newbie: changing an attribute of objects in a list

Ben Simuyandi bsimuyandi at africonnect.com
Wed Jun 11 11:06:01 EDT 2003


OK, I've read a bit and I've now changed the code. If anyone would like to
give feedback (better ways of doing it, where am I going wrong) that would
be appreciated:

n=0
while n < len(contents):
  def order(n, contents):
    m = 0
    while m < len(contents):
      if contents[n].getId != contents[m].getId:
        norder=int(contents[n].rights)
        morder=int(contents[m].rights)
        if norder==morder:
          if contents[n].modified < contents[m].modified:
            norder=norder+1
            <set object ordering value to norder>
            order(n, contents)
          elif contents[m].modified < contents[n].modified:
            morder=morder+1
            <set object ordering value to morder>
            order(m, contents)
      m=m+1
    n=n+1
  order(n, contents)

"Ben Simuyandi" <bsimuyandi at africonnect.com> wrote in message
news:bc72gq$cv3$1 at main.gmane.org...
> 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
>       yorder=int(y.ordering)
>       if xorder==yorder:                                    #if the
ordering
> attribute is the same then...
>         if x.age> y.age:                                       #if x is
> older than y then...
>           xorder=xorder+1
>          <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...
>           yorder=yorder+1
>          <set object ordering value to yorder>
>           check_listingorder(y, contents)
#call
> function again?
>   return contents                                                #return
the
> list?
>
> for x in contents:
#originally
> called with:
>   check_listingorder(x, contents)
>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>








More information about the Python-list mailing list