A Saturday 05 December 2009 02:06:47 Jon Olav Vik escrigué:
> I have many sorted tables of identical structure and would like to combine
>  them into a single, sorted table. Python >= 2.6 offers
>  heapq.merge(*iterators), but I cannot quite get it to work. Could somebody
>  please tell me how to convert a list of Table instances into a list of
>  iterators that heapq.merge() can use?
> 
> I'm particularly puzzled by the following (can be run after the code
>  below). Note that t[0] is a Table object.
> 
> In [74]: for row in t[0]: print row
>    ....:
> (0, 100.0)
> (3, 103.0)
> (6, 106.0)
> (9, 109.0)
> 
> In [75]: [row for row in t[0]]
> Out[75]: [(9, 109.0), (9, 109.0), (9, 109.0), (9, 109.0)]

Yup, this is a FAQ.  The `row` object is in fact an *accessor* to data, not a 
data container by itself.  This is the reason why:

for row in t[0]: print row

works well, because the accessor is *used* on every iteration.  However, in:

[row for row in t[0]]

you are only appending the *same* instance of the accessor each time.

Instead, you may want to use:

[row[:] for row in t[0]]

or, if you want NumPy objects instead of tuples:

[row.fetch_all_fields() for row in t[0]]

See http://www.pytables.org/docs/manual/ch04.html#RowClassDescr for more info.

HTH,

-- 
Francesc Alted

------------------------------------------------------------------------------
Join us December 9, 2009 for the Red Hat Virtual Experience,
a free event focused on virtualization and cloud computing. 
Attend in-depth sessions from your desk. Your couch. Anywhere.
http://p.sf.net/sfu/redhat-sfdev2dev
_______________________________________________
Pytables-users mailing list
Pytables-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pytables-users

Reply via email to