data.frame rows to a list
Like this: xy.list <- split(xy.df, seq(nrow(xy.df))) And if you want the rownames of xy.df to be the names of the output list, you can do: xy.list <- setNames(split(xy.df, seq(nrow(xy.df))), rownames(xy.df))
Like this: xy.list <- split(xy.df, seq(nrow(xy.df))) And if you want the rownames of xy.df to be the names of the output list, you can do: xy.list <- setNames(split(xy.df, seq(nrow(xy.df))), rownames(xy.df))
>>> lst = list(range(165)) >>> lst[0::10] [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160] Note that this is around 100 times faster than looping and checking a modulus for each element: $ python -m timeit -s “lst = list(range(1000))” “lst1 = [x for x in lst … Read more
>>> from itertools import groupby >>> L = [0, 0, 0, 3, 3, 2, 5, 2, 6, 6] >>> grouped_L = [(k, sum(1 for i in g)) for k,g in groupby(L)] >>> # Or (k, len(list(g))), but that creates an intermediate list >>> grouped_L [(0, 3), (3, 2), (2, 1), (5, 1), (2, 1), (6, … Read more
You can’t do this in a typesafe way. Why? Because in general we can’t know the length of a list until runtime. But the “length” of a tuple must be encoded in its type, and hence known at compile time. For example, (1,’a’,true) has the type (Int, Char, Boolean), which is sugar for Tuple3[Int, Char, … Read more
Short answer: use not set(a).isdisjoint(b), it’s generally the fastest. There are four common ways to test if two lists a and b share any items. The first option is to convert both to sets and check their intersection, as such: bool(set(a) & set(b)) Because sets are stored using a hash table in Python, searching them … Read more
The only difference on a bytecode level is that the .extend way involves a function call, which is slightly more expensive in Python than the INPLACE_ADD. It’s really nothing you should be worrying about, unless you’re performing this operation billions of times. It is likely, however, that the bottleneck would lie some place else.
You can pass a comparison function to List.sort. someObjects.sort((a, b) => a.someProperty.compareTo(b.someProperty));
Elixir has two kinds of strings: binaries (double quoted) and character lists (single quoted). The latter variant is inherited from Erlang and is internally represented as a list of integers, which map to the codepoints of the string. When you use functions like inspect and IO.inspect, Elixir tries to be smart and format a list … Read more
union(A, B, C, U) :- union(A, B, V), union(C, V, U). Your definition of union/3 can be improved by replacing … not(element(X,L)), … by … maplist(dif(X),L), … or … non_member(X, L), …. non_member(_X, []). non_member(X, [E|Es]) :- dif(X, E), non_member(X, Es). Here is a case where the difference shows: ?- union([A],[B],[C,D]). A = C, B … Read more
When describing a list, always consider using DCGs. In your case, you can very easily obtain what you want with a few simple modifications to your code: show_result(Squares,MaxRow,MaxCol, List) :- phrase(show_result(Squares,MaxRow,MaxCol,1), List). show_result(_,MaxRow,_,Row) –> { Row > MaxRow }, !. show_result(Squares,MaxRow,MaxCol,Row) –> { phrase(show_result(Squares,MaxRow,MaxCol,Row,1), Line) } , [Line], { Row1 is Row+1 }, show_result(Squares,MaxRow,MaxCol,Row1). show_result(_,_,MaxCol,_,Col) … Read more