[Solved] Why does indexing into a list with list[n] instead of list[[n]] in R not do what you would expect? [duplicate]

Because in general l[n] returns a sublist (possibly of length one), whereas l[[n]] returns an element: > lfile[2] [[1]] [1] “file2.xls” # returns SUBLIST of length one, not n’th ELEMENT > lfile[[2]] [1] “file2.xls” # returns ELEMENT From R intro manual: 6.1 Lists: It is very important to distinguish Lst[[1]] from Lst[1]. ‘[[…]]’ is the … Read more

[Solved] Create list of list of numbers. If sorting is wrong a new sublist should be created

Algorithm input = [2,5,1,4,7,3,1,2,3] output = [[]] for idx,val in enumerate(input): if idx > 0 and input[idx-1] > input[idx]: output.append([val]) else: output[-1].append(val) print output Output is [[2, 5], [1, 4, 7], [3], [1, 2, 3]] Explanation of the algorithm in words: Create an output list with an empty sublist. Enumerate over the input list. If … Read more