flatten

How to flatten a list to a list without coercion?

China☆狼群 提交于 2019-11-26 04:42:20
问题 I am trying to achieve the functionality similar to unlist, with the exception that types are not coerced to a vector, but the list with preserved types is returned instead. For instance: flatten(list(NA, list(\"TRUE\", list(FALSE), 0L)) should return list(NA, \"TRUE\", FALSE, 0L) instead of c(NA, \"TRUE\", \"FALSE\", \"0\") which would be returned by unlist(list(list(NA, list(\"TRUE\", list(FALSE), 0L)) . As it is seen from the example above, the flattening should be recursive. Is there a

Flatten an irregular list of lists

南笙酒味 提交于 2019-11-26 03:11:41
问题 Yes, I know this subject has been covered before (here, here, here, here), but as far as I know, all solutions, except for one, fail on a list like this: L = [[[1, 2, 3], [4, 5]], 6] Where the desired output is [1, 2, 3, 4, 5, 6] Or perhaps even better, an iterator. The only solution I saw that works for an arbitrary nesting is found in this question: def flatten(x): result = [] for el in x: if hasattr(el, \"__iter__\") and not isinstance(el, basestring): result.extend(flatten(el)) else:

What is the difference between flatten and ravel functions in numpy?

自古美人都是妖i 提交于 2019-11-26 02:26:59
问题 import numpy as np y = np.array(((1,2,3),(4,5,6),(7,8,9))) OUTPUT: print(y.flatten()) [1 2 3 4 5 6 7 8 9] print(y.ravel()) [1 2 3 4 5 6 7 8 9] Both function return the same list. Then what is the need of two different functions performing same job. 回答1: The current API is that: flatten always returns a copy. ravel returns a view of the original array whenever possible. This isn't visible in the printed output, but if you modify the array returned by ravel, it may modify the entries in the

How to make a flat list out of list of lists?

99封情书 提交于 2019-11-26 01:17:16
问题 I wonder whether there is a shortcut to make a simple list out of list of lists in Python. I can do that in a for loop, but maybe there is some cool \"one-liner\"? I tried it with reduce() , but I get an error. Code l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]] reduce(lambda x, y: x.extend(y), l) Error message Traceback (most recent call last): File \"<stdin>\", line 1, in <module> File \"<stdin>\", line 1, in <lambda> AttributeError: \'NoneType\' object has no attribute \'extend\' 回答1: Given a

How to Flatten a Multidimensional Array?

对着背影说爱祢 提交于 2019-11-25 22:14:00
问题 Is it possible, in PHP, to flatten a (bi/multi)dimensional array without using recursion or references? I\'m only interested in the values so the keys can be ignored, I\'m thinking in the lines of array_map() and array_values() . 回答1: You can use the Standard PHP Library (SPL) to "hide" the recursion. $a = array(1,2,array(3,4, array(5,6,7), 8), 9); $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($a)); foreach($it as $v) { echo $v, " "; } prints 1 2 3 4 5 6 7 8 9 回答2: As of PHP

Merge/flatten an array of arrays

时光怂恿深爱的人放手 提交于 2019-11-25 21:36:30
问题 I have a JavaScript array like: [[\"$6\"], [\"$12\"], [\"$25\"], [\"$25\"], [\"$18\"], [\"$22\"], [\"$10\"]] How would I go about merging the separate inner arrays into one like: [\"$6\", \"$12\", \"$25\", ...] 回答1: You can use concat to merge arrays: var arrays = [ ["$6"], ["$12"], ["$25"], ["$25"], ["$18"], ["$22"], ["$10"] ]; var merged = [].concat.apply([], arrays); console.log(merged); Using the apply method of concat will just take the second parameter as an array, so the last line is