A Tuesday 10 June 2008, Swank, Craig escrigué: > Hello, > When I make a list with list comprehensions, I get what I believe are > incorrect results. I made a short example that shows what I'm > talking about. I'm not sure if this is a bug in the iterator, or if > this behavior is by design. > > Thanks, > > Craig > > *************************************************** > > import tables, random > > class Example(tables.IsDescription): > col1 = tables.Int32Col(pos=1) > col2 = tables.Int32Col(pos=2) > > hFile = tables.openFile('test.h5', 'w') > table = hFile.createTable('/', 'table', Example) > entry = table.row > > for i in range(40): > entry['col1'] = random.randint(0, 5) > entry['col2'] = random.randint(0, 5) > entry.append() > > table.flush() > > for i in table.where('col1 == 2'): > print i > > """gave: > (2, 5) > (2, 4) > (2, 4) > (2, 5) > (2, 0) > that is fine > """ > > l = [i for i in table.where('col1 == 2')] > > print l > > """gave: > [(5, 2), (5, 2), (5, 2), (5, 2), (5, 2)] > that is no good > """ > > cond = table.where('col1 == 2') > > l = [i for i in cond] > > print l > > """gave: > [(5, 2), (5, 2), (5, 2), (5, 2), (5, 2)] > that is also no good > """
Yes, this is expected: you are referencing the row iterator, but without specifying which value do you want to get. Use i[:] or i.fetch_all_fields() (see the "Row methods" section of the chapter 4 of UG) for getting what you want. > > print cond > > """ and this gave: > (5, 2) > this is really unexpected (for me) > """ > > hFile.close() Well, this is the same case as before, because what you are printing is a representation of the iterator itself, and not a specific value of it (which, OTOH, makes no sense out of a loop). Cheers, -- Francesc Alted Freelance developer Tel +34-964-282-249 ------------------------------------------------------------------------- Check out the new SourceForge.net Marketplace. It's the best place to buy or sell services for just about anything Open Source. http://sourceforge.net/services/buy/index.php _______________________________________________ Pytables-users mailing list Pytables-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/pytables-users