How to count the number of underscores and split the string on the middle one only?

时间:2019-01-18 18:58:15

标签: r string

I would like to count the number of underscores and split the string into two different strings at the middle underscore.

strings <- c('aa_bb_cc_dd_ee_ff', 'cc_hh_ff_zz", "bb_dd")

Desired Output:

First        Last
"aa_bb_cc"   "dd_ee_ff"
"cc_hh"      "ff_zz"
"bb"         "dd"

3 个答案:

答案 0 :(得分:3)

这是一个笨拙的解决方案,它假定下划线始终为奇数。

# Load libraries
library(stringr)

# Define function
even_split <- function(s){
  # Split string
  tmp <- str_split(s, "_")

  lapply(tmp, function(x){
    # Patch string back together in two pieces
    c(paste(x[1:(length(x)/2)], collapse = "_"),
      paste(x[(1+length(x)/2):length(x)], collapse = "_"))
  })
}

# Example
strings <- c('aa_bb_cc_dd_ee_ff', 'cc_hh_ff_zz', 'bb_dd')

# Test function
even_split(strings)
#> [[1]]
#> [1] "aa_bb_cc" "dd_ee_ff"
#> 
#> [[2]]
#> [1] "cc_hh" "ff_zz"
#> 
#> [[3]]
#> [1] "bb" "dd"

reprex package(v0.2.1)于2019-01-18创建

答案 1 :(得分:2)

适应nhahtdh的答案here,您需要做的就是添加一个步骤来计算下划线(此处用with open('my_list.txt', 'w') as file: file.write(str(my_list)) with open('my_list.txt', 'r') as file: my_list_back = ast.literal_eval(file.read()) 完成)并返回下划线的中位数。

str_count

答案 2 :(得分:1)

假设下划线的个数为奇数,且下划线为99个或更少。

__consumer_offsets