R 中的压缩或枚举?
这些Python列表推导式的R等价物是什么:
[(i,j) for i,j in zip(index, Values)]
[(i,j) for i,j in enumerate(Values)]
[(i,j) for i,j in enumerate(range(10,20))] %MWE, indexing or enumerating to
%keep up with the index, there may
%be some parameter to look this up
输出示例
>>> [(i,j) for i,j in enumerate(range(10,20))]
[(0, 10), (1, 11), (2, 12), (3, 13), (4, 14), (5, 15), (6, 16), (7, 17), (8, 18), (9, 19)]
我之前用R中的一些技巧解决了这个问题,但不再记得了,第一个想法是itertools -pkg,但我希望找到一种更惯用的做事方式。
What are the R equivalents for these Python list comprehensions:
[(i,j) for i,j in zip(index, Values)]
[(i,j) for i,j in enumerate(Values)]
[(i,j) for i,j in enumerate(range(10,20))] %MWE, indexing or enumerating to
%keep up with the index, there may
%be some parameter to look this up
Example with Output
>>> [(i,j) for i,j in enumerate(range(10,20))]
[(0, 10), (1, 11), (2, 12), (3, 13), (4, 14), (5, 15), (6, 16), (7, 17), (8, 18), (9, 19)]
I have solved this problem earlier with some trick in R but cannot remember anymore, the first idea was itertools -pkg but I am hoping to find a more idiomatic way of doing things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
python
enumerate
的答案:在 R 中,列表是有序的(请参阅 这个答案)。因此,您所需要做的就是为键(使用
names()[i]
)或值(使用[[i]]
)建立索引。使用
seq_along
(或者可以做for(i in 1:length(mylist)){...}
):python
zip
的答案:请参阅上述答案之一来模拟元组列表。我更喜欢 BondedDust 的答案中所示的数据框:
Answer for python
enumerate
:In R, a list is ordered (see this answer). Thus, all you need is to index either keys (using
names()[i]
) or values (using[[i]]
).Using
seq_along
(alternatively can dofor(i in 1:length(mylist)){...}
):Answer for python
zip
:See one of the above answers to mimic the list of tuples. My preference is towards a data frame as shown in BondedDust's answer:
关于 R 的列表理解有一些讨论,例如 此处 或 那里。 hash 包甚至提供类似字典的结构。然而,正如其他人所说,很难尝试将一种语言设施映射到另一种语言设施上(即使这是比较编程语言实际上提供了),但不清楚它应该用来做什么。例如,我可以在 R 中模仿 Python
zip()
,如下所示:Python
R
可以看出,这实际上取决于您想要什么与事后的结果有关。
There have been some discussions around list comprehension for R, e.g. here or there. The hash package even offers dictionary-like structure. However, as others said, it is difficult to try to map one language facilities onto another (even if this is what Comparison of programming languages actually offers) without a clear understanding of what it is supposed to be used to. For example, I can mimic Python
zip()
in R as follows:Python
R
As can be seen, this really depends on what you want to do with the result afterwards.
zip
和enumerate
在 R 中实现起来并不是特别困难:枚举很容易用
zip
来定义:由于这些是正确的函数,我们可以使用
...
使它们相当灵活和简洁,并利用 mapply 的行为,例如回收输入和正确命名输出。zip
andenumerate
are not particularly difficult to implement in R:Enumerate is simple to define in terms of
zip
:Since these are proper functions, we can use
...
to make them fairly flexible and terse, and take advantage of mapply's behavior, such as recycling inputs and naming output correctly.创建向量列表的另一个选项是使用 Map 函数,如 @peterhurford 所示: https://rdrr.io/github/peterhurford/funtools/src/R/zippers.R
Another option which will create a list of vectors is to use the Map function as seen here by @peterhurford: https://rdrr.io/github/peterhurford/funtools/src/R/zippers.R
如果这是矩阵的 Python 打印表示,那么这段代码:
对于我们这些非 Python 用户来说,您仍然对所需输出的结构一无所知。您使用术语“列表”,但输出表明一组有序的元组。
根据 @chi 的指导,我们可能还建议使用以 R 为中心的“数据框架”结构
……它在列类型方面具有列表的灵活性,在行和列索引方面具有矩阵的访问功能。或者,可以使用 hhh 的请求并使用默认从“1”开始的
rownames
向量创建 j 向量的隐式索引值10:20
,但是它可以被更改为从“0”开始的字符向量不幸的是,粗心的人会发现 dfrm[0, ] 不是一个愉快的调用,返回长度为 0 的向量。
If that is the Python print representation of a matrix, then this code:
You are still leaving those of us who are not Python users in the dark regarding the structure of your desired output. You use the term "list" but the output suggests an ordered set of tuples.
Given @chi's guidance we might also suggest using the very R-centric 'dataframe' structure
... which has the flexibility of a list in terms of column types and the access features of a matrix in terms of row and column indexing. Or one could use hhh's request and create the implicitly indexed values of the j-vector,
10:20
, using therownames
vector that starts at "1" by default, but which could be altered to become a character vector starting at "0"Unfortunately, the unwary will find that dfrm[0, ] is not a happy call, returning vector of length 0.
为了将 Python 风格的列表推导式与枚举(例如枚举列表)一起使用,一种方法是安装列表推导式包 LC(2018 年开发)和 itertools 包(2015 年开发)。
R 中的列表推导式
您可以在此处<找到
LC
包< /a>.示例
,其中编程语法尚未像 Python 那样干净和完善,但功能正常,其帮助概述:
其中请注意,您可以将谓词保留为空,例如在上面的示例中。
Python 风格的 itertools 和枚举
您可以使用 R 的 itertools,它与 Python 的 itertools 非常相似itertools,进一步在 Cran 此处
描述的地方
示例.枚举
示例.带 ZIP 的枚举
In order to use Python style list comprehensions with enumerations, such as enumerated lists, one way is to install List-comprehension package
LC
(developed 2018) and itertools package (developed 2015).List comprehensions in R
You can find the
LC
package here.Example
where the programming syntax is not yet as clean and polished as in Python but functionally working and its help outlines:
where notice that you can leave the predicates empty for example in the above example.
Python-style itertools and enumerations
You can use R's itertools that is very similar to Python's itertools, further in Cran here
where described
Example. enumeration
Example. enumeration with ZIP
这可以使用两个粘贴语句来实现:
输出如下:
This can be achieved using two paste statements:
Output will like following:
对于 python,R 中的“枚举”等效项。将向量存储在列表中并使用索引迭代它们应该可以正常工作。
输出:
R“列表”是一个很好的存储库,用于存储向量并保留索引。
For python, 'enumerate' equivalent in R. Storing the vectors in the list and iterating over them with an index should work fine.
Output:
R 'list' is a nice bank for depositing the vectors and retain with an index.