flatten

Is it possible to flatten MongoDB result query?

杀马特。学长 韩版系。学妹 提交于 2019-11-28 17:24:44
问题 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: {"data" : { "country" : [ { "neighbor" : [ { "name" : "Austria" }, { "name" : "Switzerland" } ] }, { "neighbor" : { "name" : "Malaysia" } }, { "neighbor" : [ { "name" : "Costa Rica" }, { "name" : "Colombia" } ] } ] }} Now, this is what I want: ['Austria', 'Switzerland', 'Malaysia', 'Costa Rica',

Flatten Adjacency List Hierarchy To A List Of All Paths

*爱你&永不变心* 提交于 2019-11-28 16:55:23
I have a Table that stores Hierarchical information using the Adjacency List model. (uses a self referential key - example below. This Table may look familiar ): category_id name parent ----------- -------------------- ----------- 1 ELECTRONICS NULL 2 TELEVISIONS 1 3 TUBE 2 4 LCD 2 5 PLASMA 2 6 PORTABLE ELECTRONICS 1 7 MP3 PLAYERS 6 8 FLASH 7 9 CD PLAYERS 6 10 2 WAY RADIOS 6 What is the best method to "flatten" the above data into something like this? category_id lvl1 lvl2 lvl3 lvl4 ----------- ----------- ----------- ----------- ----------- 1 1 NULL NULL NULL 2 1 2 NULL NULL 6 1 6 NULL NULL 3

Flatten FDF / XFDF forms to PDF in PHP with utf-8 characters

老子叫甜甜 提交于 2019-11-28 10:59:06
My scenario: A PDF template with formfields: template.pdf An XFDF file that contains the data to be filled in: fieldData.xfdf Now I need to have these to files combined & flattened. pdftk does the job easily within php: exec("pdftk template.pdf fill_form fieldData.xfdf output flatFile.pdf flatten"); Unfortunately this does not work with full utf-8 support. For example: Cyrillic and greek letters get scrambled. I used Arial for this, with an unicode character set. How can I accomplish to flatten my unicode files? Is there any other pdf tool that offers unicode support? Does pdftk have an

How to flatten a list of tuples into a pythonic list

折月煮酒 提交于 2019-11-28 10:25:39
Given the following list of tuples: INPUT = [(1,2),(1,),(1,2,3)] How would I flatten it into a list? OUTPUT ==> [1,2,1,1,2,3] Is there a one-liner to do the above? Similar: Flatten list of Tuples in Python You could use a list comprehension : >>> INPUT = [(1,2),(1,),(1,2,3)] >>> [y for x in INPUT for y in x] [1, 2, 1, 1, 2, 3] >>> itertools.chain.from_iterable is also used a lot in cases like this: >>> from itertools import chain >>> INPUT = [(1,2),(1,),(1,2,3)] >>> list(chain.from_iterable(INPUT)) [1, 2, 1, 1, 2, 3] >>> That's not exactly a one-liner though. >>> INPUT = [(1,2),(1,),(1,2,3)] >

Flattening only one level of a list in Prolog

旧巷老猫 提交于 2019-11-28 10:05:13
问题 I'm working on a problem to flatten only one level of a list in Prolog. For example, [[1],[2,3]] would become [1,2,3] , but [[1,[2]],3] would only flatten down to [1,[2],3] . I went through some other questions on the site, but none thoroughly answered this question, and I just can't get my code to work on all my test cases. Update: the code works! Here is the eventual answer that I came to: my_flatten([], []). my_flatten([A|B],L) :- is_list(A), my_flatten(B,B1), !, append(A,B1,L). my_flatten

Flatten array with objects into 1 object

时光怂恿深爱的人放手 提交于 2019-11-28 09:38:05
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. 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's worth pointing out it comes with a _.assign function for this purpose that does the same thing: var

Depth-first flattened collection of an object hierarchy using LINQ

廉价感情. 提交于 2019-11-28 08:58:50
问题 I have an object hierarchy (MasterNode -> ChildNodes) where master and child nodes are of the same type, and there are only two levels (top level and children) like this ('A' is parent of D,E and F, 'B' is parent of G, etc) A--+ | D | E | F | B--+ | G | C--+ H I Suppose I have a MasterNodes as an IEnumerable of the parent objects (A,B,C) and given a parent object X I can get an IEnumerable of its children by X.children I know that I can enumerate all of the leaf (child nodes) with the

AutoMapper and flattening nested arrays

一笑奈何 提交于 2019-11-28 07:47:48
I'm trying to use AutoMapper to flatten multiple levels of arrays. Consider the following source classes: class X { public string A { get; set; } public Y[] B { get; set; } } class Y { public string C { get; set; } public Z[] D { get; set; } } class Z { public string E { get; set; } public string F { get; set; } } And the following destination: class Destination { public string A { get; set; } public string C { get; set; } public string E { get; set; } public string F { get; set; } } What I'd like to be able to do is get a List from one or more X, e.g.: Mapper.Map<IEnumerable<X>, IEnumerable

How to flatten a List of Futures in Scala

我们两清 提交于 2019-11-28 06:46:11
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 the flatten method isn't appropriate here, that's fine. As long as I get to the result. Thanks! Gabriele Petronella Future.sequence takes a List[Future[T]] and returns a Future[List[T]] . You can do Future.sequence(f) and then use map or onComplete on it to access the list of values. 来源: https://stackoverflow.com/questions/26717249/how-to-flatten-a-list-of-futures-in-scala

How to transpose an array more Swiftly?

荒凉一梦 提交于 2019-11-28 06:01:07
问题 I asked a similar question a while ago. It was asking how can I turn an array like this: [[1,2,3],[4,5,6],[7,8,9]] to this: [1,2,3,4,5,6,7,8,9] But now I want to turn that same array to this: [1,4,7,2,5,8,3,6,9] Assume all the subarrays have the same length. If you haven't noticed already, the first three items in the result is the first item of the three subarrays. The fourth, fifth and sixth items in the result is the second item of each subarray. If you still don't understand, maybe this