Split array into chunks
Split array into chunks
Split array into chunks
Several alternatives: 1) two ways with data.table: library(data.table) # method 1 (preferred) setDT(v)[, lapply(.SD, function(x) unlist(tstrsplit(x, “,”, fixed=TRUE))), by = AB ][!is.na(director)] # method 2 setDT(v)[, strsplit(as.character(director), “,”, fixed=TRUE), by = .(AB, director) ][,.(director = V1, AB)] 2) a dplyr / tidyr combination: library(dplyr) library(tidyr) v %>% mutate(director = strsplit(as.character(director), “,”)) %>% unnest(director) 3) with … Read more
Just use the appropriate method: String#split(). String string = “004-034556”; String[] parts = string.split(“-“); String part1 = parts[0]; // 004 String part2 = parts[1]; // 034556 Note that this takes a regular expression, so remember to escape special characters if necessary. there are 12 characters with special meanings: the backslash \, the caret ^, the … Read more
I use this to split string by a delimiter. The first puts the results in a pre-constructed vector, the second returns a new vector. #include <string> #include <sstream> #include <vector> #include <iterator> template <typename Out> void split(const std::string &s, char delim, Out result) { std::istringstream iss(s); std::string item; while (std::getline(iss, item, delim)) { *result++ = … Read more
Here’s a generator that yields the chunks you want: def chunks(lst, n): “””Yield successive n-sized chunks from lst.””” for i in range(0, len(lst), n): yield lst[i:i + n] import pprint pprint.pprint(list(chunks(range(10, 75), 10))) [[10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, … Read more