提问人:Sharif Amlani 提问时间:10/5/2023 最后编辑:Sharif Amlani 更新时间:10/5/2023 访问量:19
rpy2:如何使 ListVector 具有 1 个以上的元素?
rpy2: How to make ListVector with more than 1 elements?
问:
我正在使用 python 中的 rpy2 包在列表中创建一个列表,然后通过 R 将其传回。
我能够成功地让它生成一个包含 1 个元素的列表,但现在我希望它生成一个包含多个元素的列表,这样当我在 python 中调用我的列表对象时,它会说:ListVector with 2 elements.
这是我正在处理的代码:
import rpy2.robjects as ro
JobIDs = [2201795, 2201783]
r_list = ["a", "b", "c"]
nested_r_lists = [] # Initialize an empty list outside the loop
counter = 0
for i in JobIDs:
print(i)
# Create a new ListVector for each job id
list_vector = ro.ListVector({'Excel_List' + str(i): r_list})
# Append the ListVector to nested_r_lists
nested_r_lists.extend([list_vector])
# Now nested_r_lists contains two elements, one for each job id
nested_r_lists
里面:是这样说的:nested_r_lists
[<rpy2.robjects.vectors.ListVector object at 0x000001DE120810D0> [19]
R classes: ('list',)
[ListSexpVector]
Excel_List2201795: <class 'rpy2.rinterface.ListSexpVector'>
<rpy2.rinterface.ListSexpVector object at 0x000001DE11A26650> [19],
<rpy2.robjects.vectors.ListVector object at 0x000001DE121D1410> [19]
R classes: ('list',)
[ListSexpVector]
Excel_List2201783: <class 'rpy2.rinterface.ListSexpVector'>
<rpy2.rinterface.ListSexpVector object at 0x000001DE12128750> [19]]
但是当我打电话时,它报告了nested_r_lists[0]
nested_r_lists[1]
ListVector with 1 elements.
Excel_List2201795 [19]
ListVector with 1 elements.
Excel_List2201783 [19]
相反,我想调用并让输出为:nested_r_lists
ListVector with 2 elements.
Excel_List2201795 [19]
Excel_List2201783 [19]
请帮忙!谢谢!
答: 暂无答案
评论