【问题标题】:transforming R function output to match format of Python function output将 R 函数输出转换为匹配 Python 函数输出的格式
【发布时间】:2020-02-10 04:36:05
【问题描述】:

将 Python 函数转换为 R 我尝试在迭代使用 R 中的 strsplit() 拆分的值时添加更多数据。原始 Python 函数的返回值是列表列表,因此这在 R 中应该是相同的版本(向量的向量)。 Python函数是

anbaudauer = 12
planzen_list = [
                    'tomaten_6',
                    'karotten_4',
                    'paprika_7',
                    'erdbeeren_5',
                    'koriander_2',
                    'salat_3',
                    'zucchini_4',
                    'gurke_5',
                    'petersilie_2',
                    'radieschen_3'
                ]

def possible_combinations(values, target, with_replacement=True):
    def sub_combinations(index, l, r, t, w):
        if t == sum(filter(lambda i: isinstance(i, int), l)):
            r.append(l)
        elif t < sum(filter(lambda i: isinstance(i, int), l)):
            return
        for u in range(index, len(values)):
            sub_combinations(u if w else (u + 1), l + [values[u].split('_')[0], int(values[u].split('_')[1])], r, t, w)
        return r
    return sub_combinations(0, [], [], target, with_replacement)

raw_combinations = possible_combinations(planzen_list, anbaudauer)

# returns [['tomaten', 6, 'tomaten', 6], ['tomaten', 6, 'karotten', 4, 'koriander', 2], ['tomaten', 6, 'karotten', 4, 'petersilie', 2], ...]

R函数的当前状态是

w <- c(
  'tomaten_6',
  'karotten_4',
  'paprika_7',
  'erdbeeren_5',
  'koriander_2',
  'salat_3',
  'zucchini_4',
  'gurke_5',
  'petersilie_2',
  'radieschen_3'
)
n <- length(w)
t <- 12
D <- list()
for (j in 0:n) D[[paste(0, j)]] <- list(c())
for (i in 1:t) D[[paste(i, 0)]] <- list()
for (j in 1:n) {
  for (i in 1:t) {
    D[[paste(i, j)]] <- do.call(c, lapply(0:floor(i/as.numeric(strsplit(w[j], '_')[[1]][2])), function(r) {
      lapply(D[[paste(i-r*as.numeric(strsplit(w[j], '_')[[1]][2]), j-1)]], function(x) c(x, rep(strsplit(w[j], '_')[[1]][1]), r))
    }))
  }
}
D[[paste(t, n)]]

as.numeric(strsplit(w[j], '_')[[1]][2])我想在数字前加上蔬菜的名字。本质上,R 函数应该返回与 Python 函数相同的值。到目前为止,转换 R 函数的返回值非常棘手。基本上我试着把所有东西都拆开然后再缝合在一起。由于我很长时间没有使用 R,我确信有一种方便的方法可以获取与 Python 函数返回的数据相同的数据。

【问题讨论】:

  • strsplit 是矢量化的。您可以直接使用strsplit(w, '_') 而不是循环

标签: python r


【解决方案1】:

为什么不在这里使用好的旧正则表达式?首先:

#first drop the text before the '_' 
w_int <- as.integer(gsub(pattern = ".*_", replacement = "", w))
#repeat the replacement for the numbers at the end to get the vegetable names
w_names <- gsub(pattern = '_[0-9]*$', "", w)
#assign the vegetable names to the Anbaudauer-vector as names 
names(w_int) <- w_names 
w_int
tomaten   karotten    paprika  erdbeeren  koriander      salat ...
     6          4          7          5          2          3 ...
Anbaudauer <- 12

然后从那里检查所有加起来为 Anbaudauer 的组合。可能是这样的......combinations of numbers to reach a given sum - recursive implementation in R不想在这里重复。

【讨论】:

  • 谢谢,这里的问题是一些植物具有相同的 Anbaudauer(例如 koriander 和 petersilie)并且植物可以重复使用。建议的实现正确地给出了 103 种可能的组合,但我希望包含植物名称,而不仅仅是与 Anbaudauer 相加的数字,因为植物是我正在寻找的区别因素。
  • 您始终可以通过 names() 函数访问向量名称属性;此外, l|s|v*apply 系列函数有一个可用的 USE.NAMES = TRUE|FALSE 参数,其中可以保留名称 - 以防您在链接答案中重用该函数;我认为还有一些包“purrr”功能可以帮助解决这个问题
  • 但是使用 w_int 作为函数的输入并没有得到正确的结果,因为它忽略了例如2 天的植物发生两次。使用w_int 作为函数的输入只得到 50 个组合而不是 103 个。
  • 嗨……你试过用combn(w_int, min(which(cumsum(w_int)&gt;Anbaudauer), simplify = FALSE)之类的东西
  • 我也不确定您如何在上下文中正确定义 - 因为您最初询问如何拆分名称和数字。问题有所改变,或者是一个不同/新的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多