将 RPy2 ListVector 转换为 Python 字典 [英] Converting an RPy2 ListVector to a Python dictionary
问题描述
与 R 中的命名列表等价的自然 Python 是一个 dict,但是 RPy2 给你一个 ListVector 对象.
The natural Python equivalent to a named list in R is a dict, but RPy2 gives you a ListVector object.
import rpy2.robjects as robjects
a = robjects.r('list(foo="barbat", fizz=123)')
此时,a 是 ListVector 对象.
At this point, a is a ListVector object.
<ListVector - Python:0x108f92a28 / R:0x7febcba86ff0>
[StrVector, FloatVector]
foo: <class 'rpy2.robjects.vectors.StrVector'>
<StrVector - Python:0x108f92638 / R:0x7febce0ae0d8>
[str]
fizz: <class 'rpy2.robjects.vectors.FloatVector'>
<FloatVector - Python:0x10ac38fc8 / R:0x7febce0ae108>
[123.000000]
我想要的是我可以像普通 Python 字典一样对待的东西.我的临时黑客是这样的:
What I'd like to have is something I can treat like a normal Python dictionary. My temporary hack-around is this:
def as_dict(vector):
"""Convert an RPy2 ListVector to a Python dict"""
result = {}
for i, name in enumerate(vector.names):
if isinstance(vector[i], robjects.ListVector):
result[name] = as_dict(vector[i])
elif len(vector[i]) == 1:
result[name] = vector[i][0]
else:
result[name] = vector[i]
return result
as_dict(a)
{'foo': 'barbat', 'fizz': 123.0}
b = robjects.r('list(foo=list(bar=1, bat=c("one","two")), fizz=c(123,345))')
as_dict(b)
{'fizz': <FloatVector - Python:0x108f7e950 / R:0x7febcba86b90>
[123.000000, 345.000000],
'foo': {'bar': 1.0, 'bat': <StrVector - Python:0x108f7edd0 / R:0x7febcba86ea0>
[str, str]}}
那么,问题是...有没有更好的方法或我应该使用的 RPy2 内置的东西?
So, the question is... Is there a better way or something built into RPy2 that I should be using?
推荐答案
我认为将一个 r 向量放入 dictionary
中不必如此复杂,这个怎么样:
I think to get a r vector into a dictionary
does not have to be so involving, how about this:
In [290]:
dict(zip(a.names, list(a)))
Out[290]:
{'fizz': <FloatVector - Python:0x08AD50A8 / R:0x10A67DE8>
[123.000000],
'foo': <StrVector - Python:0x08AD5030 / R:0x10B72458>
['barbat']}
In [291]:
dict(zip(a.names, map(list,list(a))))
Out[291]:
{'fizz': [123.0], 'foo': ['barbat']}
当然,如果您不介意使用 pandas
,那就更容易了.结果将是 numpy.array
而不是 list
,但这在大多数情况下是可以的:
And of course, if you don't mind using pandas
, it is even easier. The result will have numpy.array
instead of list
, but that will be OK in most cases:
In [294]:
import pandas.rpy.common as com
com.convert_robj(a)
Out[294]:
{'fizz': [123.0], 'foo': array(['barbat'], dtype=object)}
这篇关于将 RPy2 ListVector 转换为 Python 字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!