list-manipulation

Is there an R equivalent to Mathematica's Sow and Reap functions for building lists?

半腔热情 提交于 2019-12-24 12:24:08
问题 Mathematica has an interesting way of incrementally building a list (or more than one list) of results that you calculate at various points of a complicated computation. I'd like to do something similar in R. In Mathematica, you can collect the list of each argument to each invocation of the Sow function during a computation by wrapping the entire calculation in call to the Reap function. Does R have any equivalent to these functions? Could you perhaps emulate it with environments and the <<-

Apply a function to a List of dataframes in R

半城伤御伤魂 提交于 2019-12-10 20:51:43
问题 I need help in how to manage lists in an iterative way. I have the following list list which is composed of several dataframes with same columns, but different number of rows. Example: [[1]] id InpatientDays ERVisits OfficeVisits Narcotics 1 a 0 0 18 1 2 b 1 1 6 1 3 c 0 0 5 3 4 d 0 1 19 0 5 e 8 2 19 3 6 f 2 0 9 2 [[2]] id InpatientDays ERVisits OfficeVisits Narcotics 7 a 16 1 8 1 8 b 2 0 8 0 9 c 2 1 4 3 10 d 4 2 0 2 11 e 6 5 20 2 12 a 0 0 7 4 I would like to apply a function to get all the

Pythonic iteration over sliding window pairs in list?

梦想的初衷 提交于 2019-12-10 02:28:44
问题 What's the most Pythonic efficient way to iterate over a list in sliding pairs? Here's a related example: >>> l ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> for x, y in itertools.izip(l, l[1::2]): print x, y ... a b b d c f this is iteration in pairs, but how can we get iteration over a sliding pair? Meaning iteration over the pairs: a b b c c d d e etc. which is iteration over the pairs, except sliding the pair by 1 element each time rather than by 2 elements. thanks. 回答1: How about: for x, y in

How to remove every occurrence of sub-list from list

心不动则不痛 提交于 2019-12-09 02:24:50
问题 I have two lists: big_list = [2, 1, 2, 3, 1, 2, 4] sub_list = [1, 2] I want to remove all sub_list occurrences in big_list. result should be [2, 3, 4] For strings you could use this: '2123124'.replace('12', '') But AFAIK this does not work for lists. This is not a duplicate of Removing a sublist from a list since I want to remove all sub-lists from the big-list. In the other question the result should be [5,6,7,1,2,3,4] . Update: For simplicity I took integers in this example. But list items

Pythonic iteration over sliding window pairs in list?

你离开我真会死。 提交于 2019-12-05 03:14:50
What's the most Pythonic efficient way to iterate over a list in sliding pairs? Here's a related example: >>> l ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> for x, y in itertools.izip(l, l[1::2]): print x, y ... a b b d c f this is iteration in pairs, but how can we get iteration over a sliding pair? Meaning iteration over the pairs: a b b c c d d e etc. which is iteration over the pairs, except sliding the pair by 1 element each time rather than by 2 elements. thanks. How about: for x, y in itertools.izip(l, l[1:]): print x, y You can go even simpler. Just zip the list and the list offset by one.

Lazy “n choose k” in OCaml

試著忘記壹切 提交于 2019-12-03 12:59:54
问题 As part of a bigger problem of enumerating a set, I need to write an OCaml function 'choose' which takes a list and outputs as the list of all possible sequences of size k made up of elements of that list (without repeating sequences which can be obtained from each other by permutation). The order they are put in the end list is not relevant. For example, choose 2 [1;2;3;4] = [[1;2];[1;3];[1;4];[2;3];[2;4];[3;4]] Any ideas? I would like to have the whole thing to be lazy, outputting a lazy

Lazy “n choose k” in OCaml

北城余情 提交于 2019-12-03 03:10:52
As part of a bigger problem of enumerating a set, I need to write an OCaml function 'choose' which takes a list and outputs as the list of all possible sequences of size k made up of elements of that list (without repeating sequences which can be obtained from each other by permutation). The order they are put in the end list is not relevant. For example, choose 2 [1;2;3;4] = [[1;2];[1;3];[1;4];[2;3];[2;4];[3;4]] Any ideas? I would like to have the whole thing to be lazy, outputting a lazy list, but if you have a strict solution, that'll be very useful too. Here is a strict and suboptimal

In python, how do I take the highest occurrence of something in a list, and sort it that way?

雨燕双飞 提交于 2019-12-01 21:13:15
问题 [3, 3, 3, 4, 4, 2] Would be: [ (3, 3), (4, 2), (2, 1) ] The output should be sorted by highest count first to lowest count. In this case, 3 to 2 to 1. 回答1: data = [3, 3, 3, 4, 4, 2] result = [] for entry in set(data): result.append((entry, data.count(entry))) result.sort(key = lambda x: -x[1]) print result >>[(3, 3), (4, 2), (2, 1)] 回答2: You can use a Counter in Python 2.7+ (this recipe works on 2.5+): from collections import Counter print Counter([3, 3, 3, 4, 4, 2]).most_common() # [(3, 3),

transpose nested list

為{幸葍}努か 提交于 2019-12-01 20:10:43
问题 I have a list structure which represents a table being handed to me like this > l = list(list(1, 4), list(2, 5), list(3, 6)) > str(l) List of 3 $ :List of 2 ..$ : num 1 ..$ : num 4 $ :List of 2 ..$ : num 2 ..$ : num 5 $ :List of 2 ..$ : num 3 ..$ : num 6 And I'd like to convert it to this > lt = list(x = c(1, 2, 3), y = c(4, 5, 6)) > str(lt) List of 2 $ x: num [1:3] 1 2 3 $ y: num [1:3] 4 5 6 I've written a function that does it in a really simple manner which uses Reduce , but I feel like

Does Linq/.NET3.5 support a 'zip' method?

心已入冬 提交于 2019-12-01 05:18:19
In other languages (ruby, python, ...) I can use zip(list1, list2) which works like this: If list1 is {1,2,3,4} and list2 is {a,b,c} then zip(list1, list2) would return: {(1,a), (2,b), (3,c), (d,null)} Is such a method available in .NET's Linq extensions? .NET 4 gives us a Zip method but it is not available in .NET 3.5. If you are curious, Eric Lippert provides an implementation of Zip that you may find useful. neither implementation will fill in the missing values (or check that the lengths are the same) as the question asked. here is an implementation that can: public static IEnumerable