Questions tagged [flatten]
Flattening refers to either reducing a multi-dimensional array to a single dimension or to reducing a class and class methods to handle based function calls.
flatten
1,600
questions
5371
votes
34
answers
4.4m
views
How do I make a flat list out of a list of lists?
I have a list of lists like
[
[1, 2, 3],
[4, 5, 6],
[7],
[8, 9]
]
How can I flatten it to get [1, 2, 3, 4, 5, 6, 7, 8, 9]?
If your list of lists comes from a nested list ...
1565
votes
88
answers
1.3m
views
Merge/flatten an array of arrays
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", ...]
562
votes
54
answers
212k
views
Flatten an irregular (arbitrarily nested) list of lists
Yes, I know this subject has been covered before:
Python idiom to chain (flatten) an infinite iterable of finite iterables?
Flattening a shallow list in Python
Comprehension for flattening a sequence ...
447
votes
3
answers
135k
views
What is the difference between flatten and ravel functions in numpy?
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 ...
378
votes
31
answers
440k
views
How to Flatten a Multidimensional Array?
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 ...
267
votes
8
answers
255k
views
Turn Pandas Multi-Index into column
I have a dataframe with 2 index levels:
value
Trial measurement
1 0 13
1 3
2 4
2 ...
246
votes
5
answers
234k
views
How to flatten only some dimensions of a numpy array
Is there a quick way to "sub-flatten" or flatten only some of the first dimensions in a numpy array?
For example, given a numpy array of dimensions (50,100,25), the resultant dimensions would be (...
75
votes
4
answers
111k
views
How to flatten a pandas dataframe with some columns as json?
I have a dataframe df that loads data from a database. Most of the columns are json strings while some are even list of jsons. For example:
id name columnA ...
73
votes
4
answers
72k
views
Is it possible to flatten MongoDB result query?
I have a deeply nested collection in my MongoDB collection.
When I run the following query:
db.countries.findOne({},{'data.country.neighbor.name':1,'_id':0})
I end up with this nested result here:
...
62
votes
12
answers
59k
views
Flatten a javascript object to pass as querystring
I have a javascript object that I need to flatten into a string so that I can pass as querystring, how would I do that? i.e:
{ cost: 12345, insertBy: 'testUser' } would become cost=12345&insertBy=...
59
votes
15
answers
133k
views
JavaScript flattening an array of arrays of objects
I have an array which contains several arrays, each containing several objects, similar to this.
[[object1, object2],[object1],[object1,object2,object3]]
Here is a screenhot of the object logged to ...
56
votes
10
answers
24k
views
How to flatten array in jQuery?
How to simply flatten array in jQuery? I have:
[1, 2, [3, 4], [5, 6], 7]
And I want:
[1, 2, 3, 4, 5, 6, 7]
54
votes
7
answers
8k
views
How to flatten a list to a list without coercion?
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(...
47
votes
12
answers
41k
views
Flatten array with objects into 1 object
Given input:
[{ a: 1 }, { b: 2 }, { c: 3 }]
How to return:
{ a: 1, b: 2, c: 3 }
For arrays it's not a problem with lodash but here we have array of objects.
45
votes
15
answers
112k
views
How to "flatten" or "index" 3D-array in 1D array?
I am trying to flatten 3D array into 1D array for "chunk" system in my game. It's a 3D-block game and basically I want the chunk system to be almost identical to Minecraft's system (however, this isn'...
42
votes
2
answers
59k
views
Python list comprehension, unpacking and multiple operations
I want to unpack the tuples I create by doing the following so he the result is just one simple list. I can get the desired result in 2-3 lines but surely there is a oneliner list.comp?
>>> x ...
42
votes
6
answers
40k
views
Scala - convert List of Lists into a single List: List[List[A]] to List[A]
What's the best way to convert a List of Lists in scala (2.9)?
I have a list:
List[List[A]]
which I want to convert into
List[A]
How can that be achieved recursively? Or is there any other ...
41
votes
2
answers
16k
views
Flattening an Iterable<Iterable<T>> in Guava
Is there a flatten method in Guava - or an easy way to convert an Iterable<Iterable<T>> to an Iterable<T>?
I have a Multimap<K, V> [sourceMultimap] and I want to return all ...
41
votes
6
answers
12k
views
Un-optioning an optioned Option
Say I have a val s: Option[Option[String]]. It can thus have the following values:
Some(Some("foo"))
Some(None)
None
I want to reduce it so that the first becomes Some("foo") while the two others ...
38
votes
1
answer
25k
views
How to flatten a List of Futures in Scala
I want to take this val:
val f = List(Future(1), Future(2), Future(3))
Perform some operation on it (I was thinking flatten)
f.flatten
And get this result
scala> f.flatten = List(1,2,3)
If ...
36
votes
8
answers
78k
views
Convert array of single-element arrays to a one-dimensional array
I have this kind of an array containing single-element arrays:
$array = [[88868], [88867], [88869], [88870]];
I need to convert this to one dimensional array.
Desired output:
[88868, 88867, 88869, ...
36
votes
2
answers
39k
views
Remove names from named vector and get only the values
I have a vector like below
tmp <- c(a=1, b=2, c=3)
a b c
1 2 3
I want to flatten this vector to get only 1, 2, 3.
I tried unlist(tmp) but it still gives me the same result.
How to achieve ...
35
votes
5
answers
34k
views
Julia: Flattening array of array/tuples
In Julia vec reshapes multidimensional arrays into one-dimension arrays.
However it doesn't work for arrays of arrays or arrays of tuples.
A part from using array comprehension, is there another way ...
34
votes
4
answers
19k
views
how to do a "flat push" in javascript? [duplicate]
I want to push all individual elements of a source array onto a target array,
target.push(source);
puts just source's reference on the target list.
In stead I want to do:
for (i = 0; i < source....
33
votes
8
answers
27k
views
A better way to use AutoMapper to flatten nested objects?
I have been flattening domain objects into DTOs as shown in the example below:
public class Root
{
public string AParentProperty { get; set; }
public Nested TheNestedClass { get; set; }
}
...
33
votes
11
answers
44k
views
How to Serialize Binary Tree
I went to an interview today where I was asked to serialize a binary tree. I implemented an array-based approach where the children of node i (numbering in level-order traversal) were at the 2*i index ...
32
votes
5
answers
35k
views
Flatten multidimensional array concatenating keys [duplicate]
Possible Duplicate:
PHP convert nested array to single array while concatenating keys?
Get array's key recursively and create underscore seperated string
Please, read the whole question ...
31
votes
7
answers
42k
views
How to flatten a tuple in python
I have the following element of a list, and the list is 100 elements long.
[(50, (2.7387451803816479e-13, 219))]
How do I convert each element to look like this?
[(50, 2.7387451803816479e-13, 219)]...
30
votes
7
answers
78k
views
How to deserialize JSON into flat, Map-like structure?
Have in mind that the JSON structure is not known before hand i.e. it is completely arbitrary, we only know that it is JSON format.
For example,
The following JSON
{
"Port":
{
"@alias":...
30
votes
6
answers
20k
views
How to create an image from UILabel?
I'm currently developing a simple photoshop like application on iphone. When I want to flatten my layers, the labels are at the good position but with a bad font size.
Here's my code to flatten :
...
29
votes
4
answers
34k
views
Convert 2 dimensional array
What is selectMany.ToArray() method? Is it a built in method in C#?
I need to convert two dimensional array to one dimensional array.
29
votes
7
answers
42k
views
Flatten a list in Prolog
I've only been working with Prolog for a couple days. I understand some things but this is really confusing me.
I'm suppose to write a function that takes a list and flattens it.
?- flatten([a,[b,c]...
28
votes
6
answers
109k
views
Create a flat associative array from two columns of a 2d array and filter out blacklisted values
I am using the following foreach loop to populate a new array with filtered data.
foreach ($aMbs as $aMemb) {
$ignoreArray = array(1, 3);
if (!in_array($aMemb['ID'], $ignoreArray)) {
$...
25
votes
5
answers
17k
views
How to flatten a nested tuple?
I have a nested tuple structure like (String,(String,Double)) and I want to transform it to (String,String,Double). I have various kinds of nested tuple, and I don't want to transform each manually. ...
24
votes
4
answers
12k
views
Flatten a tree (list of lists) with one statement?
Thanks to nHibernate, some of the data structures I work with are lists within lists within lists. So for example I have a data object called "category" which has a .Children property that resolves to ...
22
votes
5
answers
14k
views
Scala: Remove none elements from map and flatten
I have a map:
Map("key1" -> Some("value1"), "key2" -> None, "key3" -> Some("value3"))
I want to remove all None elements and flatten the map. What is the easiest way to accomplish that? I ...
22
votes
3
answers
33k
views
Why and when do we need to flatten JSON objects?
I am surprised that no one on StackOverflow asked this question before.
Looking through the JSON object documentation and a quick google search did not yield satisfactory results.
What's the ...
20
votes
2
answers
52k
views
flattening nested Json in pandas data frame
I am trying to load the json file to pandas data frame.
I found that there were some nested json.
Below is the sample json:
{'events': [{'id': 142896214,
'playerId': 37831,
'teamId': 3157,
'...
20
votes
3
answers
18k
views
How can I flatten nested Results?
I'm working with a third-party library that provides tree-based data structures that I have to use "as is". The API returns Result<T, Error>. I have to make some sequential calls and ...
20
votes
2
answers
1k
views
How can I completely flatten a list (of lists (of lists) ... )
I was wondering about how I could completely flatten lists and things that contain them. Among other things, I came up with this solution that slips things that have more than one element and puts ...
19
votes
6
answers
21k
views
Flatten nested JSON using jq
I'd like to flatten a nested json object, e.g. {"a":{"b":1}} to {"a.b":1} in order to digest it in solr.
I have 11 TB of json files which are both nested and contains dots in field names, meaning ...
18
votes
7
answers
13k
views
Perl: What is the easiest way to flatten a multidimensional array?
What's the easiest way to flatten a multidimensional array ?
18
votes
1
answer
20k
views
Recursively Flatten values of nested maps in Java 8
Given a Map<String, Object>, where the values are either a String or another Map<String, Object>, how would one, using Java 8, flatten the maps to a single list of values?
Example:
Map - ...
18
votes
2
answers
22k
views
flatten a data frame
I have this nested data frame
test <- structure(list(id = c(13, 27), seq = structure(list(
`1` = c("1997", "1997", "1997", "2007"),
`2` = c("2007", "2007", "2007", "2007", "2007", "2007", "2007"))...
17
votes
2
answers
30k
views
Flatten an Array in C# [duplicate]
In C# what is the shortest code to flatten an array?
For example, I want
[[1,2],[2,3],[4,5]]
into the array
[1,2,3,4,5]
I am looking for the shortest way to do so.
17
votes
4
answers
10k
views
How can I flatten an array swiftily in Swift?
I want to turn this:
let x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
into this:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
very gracefully.
The most straightforward way, of course, is
var y = [Int]()
x.forEach { y....
16
votes
8
answers
15k
views
How to unflatten a JavaScript object in a daisy-chain/dot notation into an object with nested objects and arrays?
I want to unflatten an object like this...
var obj2 = {
"firstName": "John",
"lastName": "Green",
"car.make": "Honda",
"car.model": "Civic",
"car.revisions.0.miles": 10150,
"...
16
votes
2
answers
12k
views
loop over 2d subplot as if it's a 1-D
I'm trying to plot many data using subplots and I'm NOT in trouble but I'm wondering if there is a convenience method to do this.
below is the sample code.
import numpy as np
import math
import ...
16
votes
2
answers
10k
views
Difference between LATERAL FLATTEN(...) and TABLE(FLATTEN(...)) in Snowflake
What is the difference between the use of LATERAL FLATTEN(...) and TABLE(FLATTEN(...)) in Snowflake? I checked the documentation on FLATTEN, LATERAL and TABLE and cannot make heads or tails of a ...
16
votes
1
answer
5k
views
Why is itertools.chain faster than a flattening list comprehension?
In the context of a discussion in the comments of this question it was mentioned that while concatenating a sequence of strings simply takes ''.join([str1, str2, ...]), concatenating a sequence of ...