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

查看所有标签

猜你喜欢:

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

游戏改变世界

游戏改变世界

[美] 简•麦戈尼格尔(Jane McGonigal) / 闾佳 / 浙江人民出版社 / 2012-9 / 59.90元

◆《游戏改变世界》是著名未来学家、TED大会新锐演讲者简•麦戈尼格尔探索互联时代重要趋势的最新力作。在书中,作者指出:游戏可以弥补现实世界的不足和缺陷,游戏化可以让现实变得更美好。 ◆作者在书中用大量事例告诉我们,游戏击中了人类幸福的核心,提供了令人愉悦的奖励、刺激性的挑战和宏大的胜利,而这些都是现实世界十分匮乏的。她的研究表明,我们可以借助游戏的力量,让生活变得像游戏一样精彩。 ◆作......一起来看看 《游戏改变世界》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

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

RGB CMYK 互转工具