On 20/12/10 17:18, Peter wrote:
> On Mon, Dec 20, 2010 at 3:27 PM, Luca Beltrame<[email protected]> wrote:
>> Hello,
>>
>> As I'm working with R's lists and porting some code away from R to Python,
>> I'd
>> like to know if they can be replaced with a named vector (which is what a R
>> list essentially is, to my untrained eye). For example:
>>
>> in R
>>
>> my.list<- list(a="some.value", b="another.value")
>>
>> in Python I would use:
>>
>> my_list = robjects.StrVector(["some.value", "another.value"])
>> my_list.names = ["a", "b"]
>>
>> Is this the right approach or is there a better way?
>>
>
> Depending on how you use the data structure, a Python dict
> (dictionary - like a Perl hash) might work (but you lose the order).
>
> As of Python 2.7, there is an ordered-dictionary class included
> in the collections module - but I don't know if rpy2 supports it.
It does (since few minutes ago, 2.2.x branch). Any class implementing
the method 'iteritems' will work, that is dict, OrderedDict,
rlike.TaggedList, rlike.OrdDict, and whatever someone may come up with.
The catch is that dict() does not guarantee the order in which keys come
up (the others do).
I'll take the opportunity to showcase on that theme a bit code for the
upcoming rpy2-2.2.0
>>> import rpy2.interactive as r
>>>> my_list = r.vectors.ListVector({'a': "some value",
'b': "other value"})
>>> tuple(my_list.names)
('a', 'b')
>>> tuple(my_list.rx2['a'])
('some value',)
>>> my_list.rx2['a'] = 22
>>> tuple(my_list.rx2['a'])
(22,)
L.
> Peter
>
> ------------------------------------------------------------------------------
> Lotusphere 2011
> Register now for Lotusphere 2011 and learn how
> to connect the dots, take your collaborative environment
> to the next level, and enter the era of Social Business.
> http://p.sf.net/sfu/lotusphere-d2d
> _______________________________________________
> rpy-list mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/rpy-list
------------------------------------------------------------------------------
Lotusphere 2011
Register now for Lotusphere 2011 and learn how
to connect the dots, take your collaborative environment
to the next level, and enter the era of Social Business.
http://p.sf.net/sfu/lotusphere-d2d
_______________________________________________
rpy-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rpy-list