flatten

How can I flatten an array swiftily in Swift?

青春壹個敷衍的年華 提交于 2019-12-01 14:46:34
问题 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.appendContentsOf($0) } But that makes the resulting array mutable, which is unnecessary. I don't like this way. I tried using reduce : let y = x.reduce([Int]()) { (array, ints) -> [Int] in array.appendContentsOf(ints) return array } But the compiler complains that array is immutable, so I can't call the

flattening nested Json in pandas data frame

前提是你 提交于 2019-12-01 10:56:30
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, 'matchId': 2214569, 'matchPeriod': '1H', 'eventSec': 0.8935539999999946, 'eventId': 8, 'eventName': 'Pass', 'subEventId': 85, 'subEventName': 'Simple pass', 'positions': [{'x': 51, 'y': 49}, {'x': 40, 'y': 53}], 'tags': [{'id': 1801, 'tag': {'label': 'accurate'}}]} I used the following code to load json into dataframe: with open('EVENTS.json') as f: jsonstr = json.load(f) df = pd.io.json.json_normalize

Flattening a JSON multi depty array in PHP

心不动则不痛 提交于 2019-12-01 08:02:48
Good morning, given the below data structure (in JSON for ease of reading) [ { "parent": "root", "active": "1", "label": "Index", "route": "/", "children": [ { "parent": "/", "active": "1", "label": "Products", "route": "/products", "children": [ { "parent": "/products", "active": "0", "label": "Test", "route": "/test" } ] } ] }, { "parent": "root", "active": "1", "label": "404", "route": "/404" }, { "parent": "root", "active": "1", "label": "Login", "route": "/login" } ] I am having major trouble returning from a function the following structure: [ { "parent": "root", "active": "1", "label":

Efficiency of flattening and collecting slices

北战南征 提交于 2019-12-01 07:36:36
问题 If one uses the standard .flatten().collect::<Box<[T]>>() on an Iterator<Item=&[T]> where T: Copy , does it: perform a single allocation; and use memcpy to copy each item to the destination or does it do something less efficient? 回答1: Box<[T]> does not implement FromIterator<&T> , so I'll assume your actual inner iterator is something that yields owned T s. FromIterator<T> for Box<[T]> forwards to Vec<T> , which uses size_hint() to reserve space for lower + 1 items, and reallocates as it

Python 3: Flattening nested dictionaries and lists within dictionaries

☆樱花仙子☆ 提交于 2019-12-01 01:19:00
I am dealing with a complex nested dictionary and list data structure. I need to flatten the data and bring all nested items to level 0. See below example for more clarity : {a:1,b:2,c:{c1:[{c11:1,c12:2,c13:3},{c21:1,c22:2,c23:3}],d1:[{d11:1,d12:2,d13:3},{d21:1,d22:2,d23:3}]},x:1,y:2} i need to flatten this to: {a:1,b:2,c_c1_c11:1, c_c1_c12:2,c_c1_c13:3,c_c1_c21:1,c_c1_c22:2,c_c1_c23:3, c_d1,d11:1...and so on} I took reference from the first answer in this post , but it can only work if i have nested dictionaries, and not if lists are nested within dictionaries and more dictionaries nested

Remove names from named vector and get only the values

十年热恋 提交于 2019-11-30 23:38:01
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 that efficiently? You just want to remove the names attribute from tmp . There are a number of ways to do that. You can unname it. unname(tmp) # [1] 1 2 3 Or use a very common method for removing names, by setting them to NULL . names(tmp) <- NULL Or strip the attributes with as.vector . as.vector(tmp) # [1] 1 2 3 Or re-concatenate it without the names. c(tmp, use.names=FALSE) # [1] 1 2 3 Or use setNames .

Remove names from named vector and get only the values

亡梦爱人 提交于 2019-11-30 15:54:59
问题 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 that efficiently? 回答1: You just want to remove the names attribute from tmp . There are a number of ways to do that. You can unname it. unname(tmp) # [1] 1 2 3 Or use a very common method for removing names, by setting them to NULL . names(tmp) <- NULL Or strip the attributes with as.vector . as.vector(tmp)

Flatten array with objects into 1 object

十年热恋 提交于 2019-11-30 13:38:24
问题 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. 回答1: Use Object.assign: let merged = Object.assign(...arr); // ES6 (2015) syntax var merged = Object.assign.apply(Object, arr); // ES5 syntax Note that Object.assign is not yet implemented in many environment and you might need to polyfill it (either with core-js, another polyfill or using the polyfill on MDN). You mentioned lodash, so it

How to Serialize Binary Tree

偶尔善良 提交于 2019-11-30 10:59:35
问题 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 for the left child and 2*i + 1 for the right child. The interviewer seemed more or less pleased, but I'm wondering what serialize means exactly? Does it specifically pertain to flattening the tree for writing to disk, or would serializing a tree also include just turning the tree into a linked

How to flatten a nested tuple?

浪子不回头ぞ 提交于 2019-11-30 04:46:07
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. Is there any convenient way to do that? If you use shapeless , this is exactly what you need, I think. There is no flatten on a Tupple. But if you know the structure, you can do something like this: implicit def flatten1[A, B, C](t: ((A, B), C)): (A, B, C) = (t._1._1, t._1._2, t._2) implicit def flatten2[A, B, C](t: (A, (B, C))): (A, B, C) = (t._1, t._2._1, t._2._2) This will flatten Tupple with