Moving list entries from one list to another

JB jblazi at hotmail.com
Mon Jul 15 04:02:33 EDT 2002


Bengt Richter wrote:

> On Sun, 14 Jul 2002 13:17:09 +0200, JB
> <jblazi at hotmail.com> wrote:
> 
>>Bengt Richter wrote:
>>
> [...]
>>> (Is this like your data? Or how does it differ?)
>>
>>This is ok. The real data are much longer strings, but it
>>is ok.
> A further question: What generates the "id" number, and
> what do you need it for besides verifying order? Do you
> need it as a key to other data locally or remotely? Or is
> your concern just guaranteeing
> unchanged order as the selection  narrows?

To tell you the truth, I do not know. Yesterday I noticed, 
that I did not need it, but it does not matter much. At the 
moment I use the following solution (it shows what I am 
after):

  #-------------------------------------------------------
  #self.rows is a list of QSimpleViewItem class 
  #instances.  The definition of this class is at the end
  #of this message. The lists <self.visible> and
  #<self.hidden> contain natural #numbers of the set
  #{0,1,...,N-1}, where N is the total number of elements
  #of self.rows. If we have an i with
  #0 <= i < N, then i is either in <self.visible> or in
  #<self.hidden>.
  #-------------------------------------------------------
  # Only those rows are shown, for which the <c>-th
  # coloumn contains the string <s> 
  def filter_out(self,c,s):
    print 'filter (1)'
    tmp = []
    for v in self.visible:
      if string.find(self.rows[v].col[c],s) == -1:
        self.hidden.append(v)
      else:
        tmp.append(v)
    self.visible = tmp
    self.hidden.sort()
    print 'filter (2)'

    self.update_sb()    #update scrollbars
    self.update_view()
 #----------------------------------------------------
  def filter_in(self,c,s):
    print 'filter (1)'
    tmp = []
    for h in self.hidden:
      if string.find(self.rows[h].col[c],s) != -1:
        self.visible.append(h)
      else:
        tmp.append(h)
    self.hidden = tmp
    self.visible.sort()
    print 'filter (2)'

    self.update_sb()
    self.update_view()

So I use two functions. <filter_out> is called when the 
search string gets longer and filter_in when it gets 
shorter. Even this is pretty fast. It seems to me that the 
append operations are expensive here (they take much time), 
but I always thought that Python lists are arrays...

> 
> If it's just order, we can probably eliminate them, and
> gain speed. If you need the id's, and the rest of the
> tuple is just a single string item, then we will gain
> speed by combining the id into the string, perhaps as a
> suffix. The gain will come from having a list of
> string-only elements so that we can search for substrings
> without a subscript operation on a tuple for every
> element, and being able to use map or filter using builtin
> functions. This may allow simpler approaches with adequate
> speed.

This I do not understand.

> Well, maybe the question was a little too broad ;-)
> My example searched first for 'B' then for 'Y' within the
> results of the first search, but it did not search for
> 'BY'.

Oh! I do not want that.

> This is an important distinction, since you will
> presumably want to refine a search for, e.g., Bach by
> incrementally refining based on 'B', 'Ba', 'Bac' and
> 'Bach', but if you want to find his Italian Concerto, you
> don't want to continue refining results by searching them
> for 'BachI', 'BachIt', 'Bachita', etc. I.e., you are
> finished with Bach and should presumably refine further
> based on 'I', 'It', 'Ita', etc. found anywhere in the
> entries you have so far.

Or search for "italian" immediately. The QListView widget 
(if you are a bit familiar with Qt) already has an 
incremental search built in but it looks only for patterns 
the strings start with.

>>Of course, only 40 or so lines can be displayed at the
>>same time. It plays no rôle if I pass list or tuples of
>>strings.
>>
> You mean no difference if you pass ('x','y','z') or
> ['x','y','z']? But I was referring to tuples including the
> integer id number. IOW, I wouldn't expect to pass
> [(24,'x'), (25,'y'), (26,'z')] to a low level single
> column dropdown list widget that I wanted to show x, y and
> z. So the question is in what form your parameters need to
> be passed.

This again I do not understand. I have defined a class:

class QSimpleViewItem:
  def __init__(self,id,color,tuple):
    self.id    = id     #for future use :)
    self.col   = tuple  #here are the strings
    self.sel   = 0      #selected or not selected
    self.color = color  #color of the line

In the <col> tuple, I shall almost always use coloumn 2 for 
my searching. But I may want to use other colomns too.

When update_view is called, my widget takes a list of 
QSimpleViewItem-s and updates the screen.

> As you notice, we are now a long way from discussing just
> "Moving list entries from one list to another" ;-)

Yes, sorry.

-- 
Janos Blazi


-----------== Posted via Newsfeed.Com - Uncensored Usenet News ==----------
   http://www.newsfeed.com       The #1 Newsgroup Service in the World!
-----= Over 100,000 Newsgroups - Unlimited Fast Downloads - 19 Servers =-----



More information about the Python-list mailing list