简体   繁体   English

使用 Python 语义对 R 中的嵌套列表进行排序

[英]Sorting Nested Lists in R with Python Semantics

I need to replicate the following sorting behavior (found in Python) in R.我需要在 R 中复制以下排序行为(在 Python 中找到)。

Assuming In Python:假设在 Python 中:

l = [(0,0), (1,-1), (-1,0), (-1,-1)]

>>> sorted(l)
[(-1, -1), (-1, 0), (0, 0), (1, -1)]

>>> min(l)
[(-1, -1)]

The equivalent data structure in R is: R 中的等效数据结构为:

l <- list(c(0,0), c(1,-1), c(-1,0), c(-1,-1))

The sort() and sort.list() methods are not implemented for non-atomic vectors.非原子向量没有实现sort()sort.list()方法。

In my use-case I can guarantee a list of length 2 vectors, so this works:在我的用例中,我可以保证长度为 2 的向量列表,所以这有效:

sorted <- function(list){
  m=matrix(unlist(list), ncol = 2, byrow = T)
  asplit(
    m[order(m[,1],m[,2]),],
    1
  )
}

Replicating the behavior of min from Python is easy, just relies on the correct function of a sorted implementation in R.从 Python 复制min的行为很容易,只依赖于 R 中sorted实现的正确 function。

min.list <- function(list) sorted(list)[1]

Advice on implementing the same behavior as sorted is greatly appreciated, and considerations for efficiency are especially welcomed.非常感谢有关实现与sorted相同行为的建议,特别欢迎考虑效率。

Unnecessary for my implementation, but an additional consideration is the sorted behavior when the sub-lists varies in length.对于我的实现来说是不必要的,但另一个考虑因素是子列表长度不同时的sorted行为。

>>> sorted([(0,0), (1,1), (0,-1), (0,-1, 0), (0,-1,-1), (0, 0, 0)])

[(0, -1), (0, -1, -1), (0, -1, 0), (0, 0), (0, 0, 0), (1, 1)]

Thanks in advance提前致谢

Maybe we can try this way也许我们可以试试这种方式

> l <- list(c(0, 0), c(1, 1), c(0, -1), c(0, -1, 0), c(0, -1, -1), c(0, 0, 0))

> l[order(sapply(l, toString))]
[[1]]
[1]  0 -1

[[2]]
[1]  0 -1 -1

[[3]]
[1]  0 -1  0

[[4]]
[1] 0 0

[[5]]
[1] 0 0 0

[[6]]
[1] 1 1

One option is to row bind to a matrix, split by column and use order() to get the indices.一种选择是将行绑定到矩阵,按列拆分并使用order()获取索引。 For ragged data, it's necessary to standardize the lengths first but obviously this step can be skipped for slightly increased efficiency if the data is guaranteed to be of equal length.对于参差不齐的数据,首先需要对长度进行标准化,但如果保证数据长度相等,显然可以跳过这一步以稍微提高效率。

l <- list(c(0, 0), c(1, 1), c(0, -1), c(0, -1, 0), c(0, -1, -1), c(0, 0, 0))

l[do.call(order, c(asplit(do.call(rbind, lapply(l,
    `length<-`, max(lengths(l)))), 2), na.last = FALSE))]

[[1]]
[1]  0 -1

[[2]]
[1]  0 -1 -1

[[3]]
[1]  0 -1  0

[[4]]
[1] 0 0

[[5]]
[1] 0 0 0

[[6]]
[1] 1 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM