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

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

内容简介: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)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

测出转化率:营销优化的科学与艺术

测出转化率:营销优化的科学与艺术

【美】高尔德(Goward,C.) / 谭磊、唐捷译 / 电子工业出版社 / 2014-10-1 / 68.00元

本书作者通过已成功实现大幅提升转化率的案例,展示了大量以营销为核心的电子商务网站的测试设计方法及转化优化方案。书中作者强调了测试及优化思维的重要性,并就实现方法做了详细讲解。 通过本书,读者将学到如何能够在网站遇到发展和收入瓶颈时,测试出存在的问题并找到解决方案;如何可以深入地了解客户需求,并以此为基础优化网站,使其达到提升转化率的目的;如何提升网站的竞争优势,把在线营销渠道变成高效的转化通......一起来看看 《测出转化率:营销优化的科学与艺术》 这本书的介绍吧!

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

正则表达式在线测试

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具