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

查看所有标签

猜你喜欢:

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

改变未来的九大算法

改变未来的九大算法

[美] 约翰.麦考密克 / 管策 / 中信出版社 / 2013-6 / 39.00元

Google得出的搜索结果是如何产生的? 百度为何会陷入“搜索门”,又是什么机制使然? 身处在大数据时代的我们,究竟该如何应对变化莫测的世界? …… 没有满篇的专业术语,第一次让我们通过简单明了的语言、生动的例证了解支撑计算机王国的灵魂支柱——9大算法,包括人工智能、数据压缩,以及Google著名的PageRank等。 本书精彩地介绍了搜索引擎、PageRank、公开......一起来看看 《改变未来的九大算法》 这本书的介绍吧!

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

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具