ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-list
python-list
Re: Moving list entries from one list to another
by Alex Martelli other posts by this author
Jul 14 2002 8:23AM messages near this date
Re: Moving list entries from one list to another | Re: Moving list entries from one list to another
JB wrote:

> > I bet you
> > can do better by exploiting the specifics of this case!-)
>  
>  Well, I can use the fact that I know the maximal sorting key
>  in advance. So I can append sentinel entries to both lists
>  and this makes things much easier.

Yes.  While this may not be the optimal solution to your
actual problem (as I notice the thread is now exploring
quite different architectures), it's getting better as a
solution to the problem you originally expressed about
"moving list entries", as per subject.  Untested code
below, but getting a bit more readable, I hope...:


au = [None] * 2
def prepare(au, alist, idx):
    alist.append(sentinel)
    it = iter(alist)
    au[idx] = [ it.next(), idx, it.next, [] ]
prepare(au, list1, 0)
prepare(au, list2, 1)

# naming the items of au's items helps readability:
ITEM, INDX, ITER, LIST = range(4)

# writing complicated expressions as functions also helps:
def moredata(au):
    return au[0][ITEM} != sentinel or au[1][ITEM} != sentinel

while moredata(au):
    m = min(au)
    i = m[INDX]
    if i and f(m[ITEM]): i = 0
    au[i][LIST].append(m[ITEM])
    m[ITEM] = m[ITER]()

# now, both lists are exhausted, so just copy the results back
list1[:] = au[0][LIST]
list2[:] = au[1][LIST]


sentinel is any value >  max(max(list1), max(list2)).


Alex

-- 
http://mail.python.org/mailman/listinfo/python-list
Thread:
JB
JB
Bengt Richter
JB
Alex Martelli
Bengt Richter
JB
JB
JB
JB
JB
JB
Bengt Richter
Alex Martelli
Alex Martelli
Alex Martelli
Emile van Sebille
=?iso-8859-1?q?Fran=E7ois?= Pinard

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved