Splitting a string by list of indices

s=”long string that I want to split up” indices = [0,5,12,17] parts = [s[i:j] for i,j in zip(indices, indices[1:]+[None])] returns [‘long ‘, ‘string ‘, ‘that ‘, ‘I want to split up’] which you can print using: print ‘\n’.join(parts) Another possibility (without copying indices) would be: s=”long string that I want to split up” indices = … Read more

Access lapply index names inside FUN

Unfortunately, lapply only gives you the elements of the vector you pass it. The usual work-around is to pass it the names or indices of the vector instead of the vector itself. But note that you can always pass in extra arguments to the function, so the following works: x <- list(a=11,b=12,c=13) # Changed to … Read more