flatten

Flatten of dict of lists into a dataframe

橙三吉。 提交于 2021-02-08 01:50:50
问题 I have a dict of lists say: data = {'a': [80, 130], 'b': [64], 'c': [58,80]} How do I flatten it and convert it into dataframe like the one below: 回答1: Use nested list comprehension with if-else if want no count one element lists: df = pd.DataFrame([('{}{}'.format(k, i), v1) if len(v) > 1 else (k, v1) for k, v in data.items() for i, v1 in enumerate(v, 1)], columns=['Index','Data']) print (df) Index Data 0 a1 80 1 a2 130 2 b 64 3 c1 58 4 c2 80 EDIT: data = {'a': [80, 130], 'b': np.nan, 'c':

How to flatten a list inside a map in Java 8

孤街浪徒 提交于 2021-02-04 18:17:05
问题 How can I go from a map of integers to lists of strings such as: <1, ["a", "b"]>, <2, ["a", "b"]> To a flattened list of strings such as: ["1-a", "1-b", "2-a", "2-b"] in Java 8 ? 回答1: You can use flatMap on values as: map.values() .stream() .flatMap(List::stream) .collect(Collectors.toList()); Or if you were to make use of the map entries, you can use the code as Holger pointed out : map.entries() .stream() .flatMap(e -> e.getValue().stream().map(s -> e.getKey() + s)) .collect(Collectors

Is js native array.flat slow for depth=1?

为君一笑 提交于 2021-02-04 16:36:45
问题 This gist is a small benchmark I wrote comparing the performance for 4 alternatives for flattening arrays of depth=1 in JS (the code can be copied as-is into the google console). If I'm not missing anything, the native Array.prototype.flat has the worst performance by far - on the order of 30-50 times slower than any of the alternatives. Update : I've created a benchmark on jsperf. It should be noted that the 4th implementation in this benchmark is consistently the most performant - often

How to flatten a 2 deep list, return a list without any sublists within

ぃ、小莉子 提交于 2021-01-28 18:52:11
问题 I was working on an algorithm that 'flattens' a list, essentially removing any sublists within a list. For example, [2, 3, [3, 4]] should become [2, 3, 3, 4]. I wrote the following to do this: def flatten(l): total = [] for i in l: if i == int: total.append(i) elif i == list: for j in i: total.append(j): return total This algorithm yields an error, however. If someone could help that would be great. Also, if someone could show a recursive path to solve this problem so that lists of arbitrary

How to flatten a 2 deep list, return a list without any sublists within

青春壹個敷衍的年華 提交于 2021-01-28 18:37:49
问题 I was working on an algorithm that 'flattens' a list, essentially removing any sublists within a list. For example, [2, 3, [3, 4]] should become [2, 3, 3, 4]. I wrote the following to do this: def flatten(l): total = [] for i in l: if i == int: total.append(i) elif i == list: for j in i: total.append(j): return total This algorithm yields an error, however. If someone could help that would be great. Also, if someone could show a recursive path to solve this problem so that lists of arbitrary

Scheme: Is it possible to convert a list of S-expressions into a list of atoms?

*爱你&永不变心* 提交于 2021-01-28 11:26:14
问题 I am trying to convert a list of S-expressions to a plain list of atoms similar to a problem in the book The Little Schemer . My code is (as typed in Dr.Racket): > (define lat '((coffee) cup ((tea) cup) (and (hick)) cup)) > (define f (lambda (lat) (cond ((null? lat) (quote ())) ((atom? (car lat)) (cons (car lat) (f (cdr lat)))) (else (cons (f (car lat)) (f (cdr lat))))))) > (f lat) '((coffee) cup ((tea) cup) (and (hick)) cup) The above code is returning back the list the same as input list. I

How to flatten heterogeneous lists (aka tuples of tuples of …)

牧云@^-^@ 提交于 2021-01-27 04:07:10
问题 I am attempting to employ C++17 fold expressions and the C++14 indices trick to flatten an arbitrary input consisting of tuples and non-tuples. The expected result should at least conform to these requirements: constexpr auto bare = 42; constexpr auto single = std::tuple{bare}; constexpr auto nested_simple = std::tuple{single}; constexpr auto multiple = std::tuple{bare, bare}; constexpr auto nested_multiple = std::tuple{multiple}; constexpr auto multiply_nested = std::tuple{multiple, multiple

How to flatten heterogeneous lists (aka tuples of tuples of …)

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-27 04:06:51
问题 I am attempting to employ C++17 fold expressions and the C++14 indices trick to flatten an arbitrary input consisting of tuples and non-tuples. The expected result should at least conform to these requirements: constexpr auto bare = 42; constexpr auto single = std::tuple{bare}; constexpr auto nested_simple = std::tuple{single}; constexpr auto multiple = std::tuple{bare, bare}; constexpr auto nested_multiple = std::tuple{multiple}; constexpr auto multiply_nested = std::tuple{multiple, multiple

Sum of 2 different 2d arrays

筅森魡賤 提交于 2020-12-27 03:04:14
问题 I need help writing a method to find out a sum of 2 different sized 2d arrays. public static int[][] summary(int[][] tab1, int[][] tab2, int x) { int[][] finalTab = new int[4][5]; // I took sizes of bigger one if (x < 0) { for (int i = 0; i < finalTab.length - 1; i++) { for (int j = 0; j < finalTab[i].length - 1; j++) { finalTab[i][j] = tab1[i][j] + tab2[i][j]; if (tab1[i][j] == 0) { finalTab[i][j] = tab2[i][j]; } } } for (int i = 0; i < finalTab.length; i++) { for (int j = 0; j < finalTab[i]

Sum of 2 different 2d arrays

吃可爱长大的小学妹 提交于 2020-12-27 03:03:37
问题 I need help writing a method to find out a sum of 2 different sized 2d arrays. public static int[][] summary(int[][] tab1, int[][] tab2, int x) { int[][] finalTab = new int[4][5]; // I took sizes of bigger one if (x < 0) { for (int i = 0; i < finalTab.length - 1; i++) { for (int j = 0; j < finalTab[i].length - 1; j++) { finalTab[i][j] = tab1[i][j] + tab2[i][j]; if (tab1[i][j] == 0) { finalTab[i][j] = tab2[i][j]; } } } for (int i = 0; i < finalTab.length; i++) { for (int j = 0; j < finalTab[i]