larry-discuss team mailing list archive
-
larry-discuss team
-
Mailing list archive
-
Message #00001
Re: New features: totuples, fromtuples
On Sun, Jan 31, 2010 at 8:01 AM, Keith Goodman <kwgoodman@xxxxxxxxx> wrote:
> I added two new methods: totuples and tolist. And two new functions:
> fromtuples and fromlist.
>
> totuples can be used, for example, to prepare a larry for insertion
> into a database:
>
> >>> y = larry([[1, 2], [3, 4]], [['a', 'b'], ['c', 'd']])
> >>> y.totuples()
> [('a', 'c', 1), ('a', 'd', 2), ('b', 'c', 3), ('b', 'd', 4)]
>
> And from tuples could be used to go in the other direction.
Added todict and fromdict:
>>> y = larry([[1.0, 2.0], [3.0, 4.0]], [['a', 'b'], ['c', 'd']])
>>> y.todict()
{('b', 'c'): 3.0, ('a', 'd'): 2.0, ('a', 'c'): 1.0, ('b', 'd'): 4.0}
This could be used to create a regular numpy array from indices:
>> data = {(0, 0): 1.0, (0, 1): 2.0, (1, 0): 3.0, (1, 1): 4.0}
>> la.fromdict(data).A
array([[ 1., 2.],
[ 3., 4.]])
References