flatten

How to flatten a hetrogenous list of list into a single list in python?

不问归期 提交于 2019-12-28 04:27:04
问题 I have a list of objects where objects can be lists or scalars. I want an flattened list with only scalars. Eg: L = [35,53,[525,6743],64,63,[743,754,757]] outputList = [35,53,525,6743,64,63,743,754,757] P.S. The answers in this question does not work for heterogeneous lists. Flattening a shallow list in Python 回答1: Here is a relatively simple recursive version which will flatten any depth of list l = [35,53,[525,6743],64,63,[743,754,757]] def flatten(xs): result = [] if isinstance(xs, (list,

Flatten xml hierarchy using XSLT

喜你入骨 提交于 2019-12-25 18:44:17
问题 I would like to convert a nested xml to flatten xml using XSLT. Incoming xml Structure would be similar but the node names would change for the incoming xmls, so would like to handle in dynamically Sample input <?xml version="1.0" encoding="UTF-8"?> <queryResponse> <Account> <Id>0010</Id> <Name>AA</Name> <RecordTypeId>0122/RecordTypeId> <RecordType> <Id>012</Id> <DeveloperName>Legal_Associate</DeveloperName> </RecordType> </Account> <Account> <Id>0011</Id> <Name>BB</Name> <RecordTypeId>0123<

Flatten json documents in Java

一个人想着一个人 提交于 2019-12-25 17:59:26
问题 I am a novice in Java and I am looking for a way to flatten json documents. I have tried Object mapper but without success. I found this link but the results is not what I need :https://github.com/wnameless/json-flattener I need to transform the documents like in the example below : Here is an example of my documents Documents recieved: { "data1": "A", "data2": "B", "data3": "C", "data4": [{ "subdata": [{ "subsubdata": "mam" }, { "subsubdata": "mom" }, { "subsubdata": "mim" }] }] } Documents

flatten list in lisp

被刻印的时光 ゝ 提交于 2019-12-25 16:22:31
问题 So I've been looking at flattening a list in lisp. However, what I wanted to do is flatten a list level by level. So instead of having (flatten '(a (b (d (g f))) e)) = (a b d g f e) i want (flatten '(a (b (d (g f))) e)) = (a b (d (g f )) e ) Any idea on how to do this guys? Much appreciated =) 回答1: You could do something like this, for example: (defun fletten-level (tree) (loop for e in tree nconc (if (consp e) (copy-list e) (list e)))) (fletten-level '(a (b (d (g f))) e)) ;; (A B (D (G F)) E

C# flattening/expanding a 3D matrix into a jagged array

北城余情 提交于 2019-12-24 15:38:41
问题 I have to flatted a 3d array in order to be serialized. Let's start with this: int[,,] array3D = new int[,,] { { { 1, 2 }, { 3, 4 }, {5,6 }, {7,8 } }, { { 9, 10}, { 11, 12},{ 13,14} , {15,16 }}, { { 17, 18}, { 19, 20},{ 21,22}, {23,24 } } }; which makes it like this (something like 1,2,3,4,...,24): So now I have this s/r public static T[] Flatten<T>(T[,,] arr) { int rows0 = arr.GetLength(0); int rows1 = arr.GetLength(1); int rows2 = arr.GetLength(2); T[] arrFlattened = new T[rows0 * rows1*

Python flatten list (but not all the way)

坚强是说给别人听的谎言 提交于 2019-12-24 12:12:48
问题 I have a list of lists of lists... A = [ [[1,3]], [[3,5], [4,4], [[5,3]]] ] the following function outputs [1, 3, 3, 5, 4, 4, 5, 3] def flatten(a): b = [] for c in a: if isinstance(c, list): b.extend(flatten(c)) else: b.append(c) return b However, I want to stop flattening on the last level so that I get [ [1,3], [3,5], [4,4], [5,3] ] 回答1: You could test for contained lists before flattening: def flatten(a): b = [] for c in a: if isinstance(c, list) and any(isinstance(i, list) for i in c): b

turn two nested associative arrays into one flat array?

╄→尐↘猪︶ㄣ 提交于 2019-12-24 03:27:49
问题 I am running two queries, they return arrays similar to what is shown below: First: array( array( 'id' => 1 ), array( 'id' => 2 ), array( 'id' => 3 ), array( 'id' => 4 ), ) Second: array( array( 'id' => 4 ), array( 'id' => 5 ), array( 'id' => 6 ), array( 'id' => 7 ), ) But I want to end up with $ids = array(1,2,3,4,5,6,7); But the only way I can think of to do that is $ids = array(); foreach(array($array1, $array2) as $a){ foreach($a as $id){ $ids[] = $id['id']; } } $ids = array_unique($ids);

Unnesting structs in BigQuery

可紊 提交于 2019-12-23 04:54:28
问题 What is the correct way to flatten a struct of two arrays in BigQuery? I have a dataset like the one pictured here (the struct.destination and struct.visitors arrays are ordered - i.e. the visitor counts correspond specifically to the destinations in the same row): I want to reorganize the data so that I have a total visitor count for each unique combination of origins and destinations. Ideally, the end result will look like this: I tried using UNNEST twice in a row - once on struct

Flatten any nested json string and convert to dataframe using spark scala

走远了吗. 提交于 2019-12-23 01:51:15
问题 I am trying to create dataframe from any json string to dataframe. The json string is generally very deep and nested some times. The json string is like: val json_string = """{ "Total Value": 3, "Topic": "Example", "values": [ { "value1": "#example1", "points": [ [ "123", "156" ] ], "properties": { "date": "12-04-19", "model": "Model example 1" } }, {"value2": "#example2", "points": [ [ "124", "157" ] ], "properties": { "date": "12-05-19", "model": "Model example 2" } } ] }""" The output

Scala nested arrays flattening

会有一股神秘感。 提交于 2019-12-22 08:16:46
问题 How to flatten an array of nested arrays of any depth ? For instance val in = Array( 1, Array(2,3), 4, Array(Array(5)) ) would be flattened onto val out = Array(1,2,3,4,5) Thanks in Advance. 回答1: If you have mixed Int and Array[Int] , which is not a very good idea to begin with, you can do something like in.flatMap{ case i: Int => Array(i); case ai: Array[Int] => ai } (it will throw an exception if you've put something else in your array). You can thus use this as the basis of a recursive