What `R` you? (R list in python)

栏目: IT技术 · 发布时间: 6年前

内容简介:Like

A R list is a python …

Like R vectors, it depends. A R list will behave differently in python depending if it is named or not.

Unnamed R list

An unnamed list in R is a python list but this does not mean R and python lists have the exact same traits. After all, they are different languages.

library(tidyverse)
library(reticulate)
conda_list()[[1]] %>% use_condaenv()

Relement_int=2L
Relement_bool=TRUE
Relement_char="banana"

Rlist_nameno<-list(Relement_int, Relement_bool, Relement_char)

class(Rlist_nameno)
## [1] "list"
r_to_py(Rlist_nameno) %>% class()
## [1] "python.builtin.list"   "python.builtin.object"

Special case for tuples

python has a structure similar to a python list, it is known as a tuple . There are no R data structures which are converted to python ’s tuple. Nonetheless, you can create a tuple directly in R and call it later in python .

Rtuple<-tuple(66,99)
class(Rtuple)
## [1] "python.builtin.tuple"  "python.builtin.object"

A tuple created in R is still a tuple when it is translated into python .

r_to_py(Rtuple) %>% class()
## [1] "python.builtin.tuple"  "python.builtin.object"

When you print a tuple created in R , it appears as a tuple with the elements sandwiched between ( ) .

Rtuple
## (66.0, 99.0)

However, when you create a tuple in python and translate it into R , the tuple gets converted into an unnamed R list.

py_run_string("Ptuple=(66,99)")

py$Ptuple
## [[1]]
## [1] 66
## 
## [[2]]
## [1] 99

Named R list

A named R list is a python dictionary

Rlist_nameyes = list(int= Relement_int, bool=Relement_bool, char=Relement_char)
class(Rlist_nameyes)
## [1] "list"
r_to_py(Rlist_nameyes) %>% class()
## [1] "python.builtin.dict"   "python.builtin.object"

In a named R list, the names of each element list are similar to the keys in a python dictionary.

names(Rlist_nameyes)
## [1] "int"  "bool" "char"
py_eval("r.Rlist_nameyes.keys()")
## dict_keys(['int', 'bool', 'char'])

The constituent elements of each element list are equivalent to the values in a python dictionary.

Rlist_nameyes
## $int
## [1] 2
## 
## $bool
## [1] TRUE
## 
## $char
## [1] "banana"
py_eval("r.Rlist_nameyes")
## $int
## [1] 2
## 
## $bool
## [1] TRUE
## 
## $char
## [1] "banana"

Creating dictionaries directly

You can create python dictionary directly in R with the dict function.

Rdict<-dict(int= Relement_int, bool=Relement_bool, char=Relement_char)
class(Rdict)
## [1] "python.builtin.dict"   "python.builtin.object"

Let’s check that python recognises the dictionary created by R as legitimate python dictionary structure.

r_to_py(Rdict) %>% class()
## [1] "python.builtin.dict"   "python.builtin.object"

A dictionary created in R prints like dictionary in python where the {} embraces the keys and values, and the keys and values are separated with : .

Rdict
## {'int': 2, 'bool': True, 'char': 'banana'}

Sub setting

In R , the sub setting approach will influence whether the name of element list and the consistent elements will be printed or just the consistent elements will be printed. The former is known as preserving sub setting and the latter is known as simplified sub setting .

  1. Preserving sub setting

The structure of input is preserved in the output. When the input is a list, the output is a list. As the output is a list, it allows both the name of the element list and its constituent elements to be printed out. In R , you wrap the name of the element list between singular square brackets [ ] .

Rlist_nameyes["int"]
## $int
## [1] 2

To achieve the same with a python dictionary would mean that given a key, the corresponding key-value pair will be printed. I’m not sure of the most elegant technique to extract specific key-value pair from a python dictionary based on a given key but I found this technique works .

py_run_string("dictfilt = lambda x, y: dict([ (i,x[i]) for i in x if i in set(y)])")

py_eval("dictfilt(r.Rlist_nameyes, ['int'])")
## $int
## [1] 2
  1. Simplified sub setting

This approach “returns the simplest possible data structure that can represent the output” . Simplified sub setting a R list will yield a vector. In other words, sub setting using the name of the element will result in only the constituent elements. The name of element list will not be printed out in the output unlike in persevered sub setting. In R , you either wrap the name of the element list between dual square brackets [[ ]]

Rlist_nameyes[["int"]]
## [1] 2

or use the dollar sign syntax $

Rlist_nameyes$int
## [1] 2

Extracting just the value in a python dictionary is done by wrapping the key between singular square bracket [ ] (notice the difference between R and python when using [ ] to subset) .

py_eval("r.Rlist_nameyes['int']")
## [1] 2

以上所述就是小编给大家介绍的《What `R` you? (R list in python)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

期货趋势程序化交易方法

期货趋势程序化交易方法

马文胜 编 / 中国财政经济 / 2008-1 / 42.00元

《期货趋势程序化交易方法》可作为学习期货行业的教程。中国期货行业非常重视期货人才队伍的建设,无论是在抓紧推进期货分析师的认证体系建设、提升期货分析师的执业水平上,还是在专业人才的后续教育上。 要想在期货市场上长期生存并保持稳定的获利,必须在充分认识市场的基础上,建立一个有效的系统化的手段和程序化的方法,把一切的复杂性和不确定性全部加以量化,使所有的交易有序而直观,才能最终达到低风险、低回报。一起来看看 《期货趋势程序化交易方法》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试