flatten

Scala - convert List of Lists into a single List: List[List[A]] to List[A]

大憨熊 提交于 2019-11-26 21:06:12
问题 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 better way? 回答1: List has the flatten method. Why not use it? List(List(1,2), List(3,4)).flatten > List(1,2,3,4) 回答2: Given the above example, I'm not sure you need recursion. Looks like you want List.flatten instead. e.g. scala> List(1,2,3) res0: List[Int] = List(1, 2, 3) scala> List(4,5,6) res1: List[Int]

Flatten a list in Prolog

旧时模样 提交于 2019-11-26 20:57:57
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],[[d],[],[e]]],Xs). Xs = [a,b,c,d,e]. % expected result The function takes out the inner structures of the list. This is what I have so far: flatten2([],[]). flatten2([Atom|ListTail],[Atom|RetList]) :- atom(Atom), flatten2(ListTail,RetList). flatten2([List|ListTail],RetList) :- flatten2(List,RetList). Now, this works when I call: ?- flatten2([a,[b,c],[[d],[],[e]]], R). R = [a,b,c,d,e]. % works as

Flatten a javascript object to pass as querystring

徘徊边缘 提交于 2019-11-26 19:33:04
问题 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=testUser I can't use jQuery AJAX call for this call, I know we can use that and pass the object in as data but not in this case. Using jQuery to flatten to object would be okay though. Thank you. 回答1: You want jQuery.param: var str = $.param({ cost: 12345, insertBy: 'testUser' }); // "cost=12345

Turn Pandas Multi-Index into column

僤鯓⒐⒋嵵緔 提交于 2019-11-26 18:24:17
I have a dataframe with 2 index levels: value Trial measurement 1 0 13 1 3 2 4 2 0 NaN 1 12 3 0 34 Which I want to turn into this: Trial measurement value 1 0 13 1 1 3 1 2 4 2 0 NaN 2 1 12 3 0 34 How can I best do this? I need this because I want to aggregate the data as instructed here , but I can't select my columns like that if they are in use as indices. CraigSF The reset_index() is a pandas DataFrame method that will transfer index values into the DataFrame as columns. The default setting for the parameter is drop=False (which will keep the index values as columns). All you have to do add

How to create an image from UILabel?

末鹿安然 提交于 2019-11-26 17:41:28
问题 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 : UIGraphicsBeginImageContext(CGSizeMake(widthDocument,widthDocument)); for (UILabel *label in arrayLabel) { [label drawTextInRect:label.frame]; } UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); Anybody can help me ? 回答1: From: pulling an UIImage from a

Python 3 replacement for deprecated compiler.ast flatten function

白昼怎懂夜的黑 提交于 2019-11-26 16:47:25
What's the recommended way to flatten nested lists since the deprecation of the compiler package ? >>> from compiler.ast import flatten >>> flatten(["junk",["nested stuff"],[],[[]]]) ['junk', 'nested stuff'] I know that there are a few stack overflow answers for list flattening, but I'm hoping for the pythonic, standard package, "one, and preferably only one, obvious way" to do this. Your stated function takes a nested list and flattens that into a new list. To flatten an arbitrarily nested list into a new list, this works on Python 3 as you expect: import collections def flatten(x): result =

Convert nested JSON array into separate columns in CSV file

青春壹個敷衍的年華 提交于 2019-11-26 16:44:15
I have a JSON file that looks like this: { "id": 10011, "title": "Test procedure", "slug": "slug", "url": "http://test.test", "email": "test@test.com", "link": "http://test.er", "subject": "testing", "level": 1, "disciplines": [ "discipline_a", "discipline_b", "discipline_c" ], "areas": [ "area_a", "area_b" ] }, I was trying to use the following command to convert that into the CSV file: (Get-Content "PATH_TO\test.json" -Raw | ConvertFrom-Json)| Convertto-CSV -NoTypeInformation | Set-Content "PATH_TO\test.csv" However, for disciplines and areas I am getting System.Object[] in the resulting CSV

How to flatten array in jQuery?

谁都会走 提交于 2019-11-26 16:11:22
问题 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] 回答1: You can use jQuery.map, which is the way to go if you have the jQuery Library already loaded. $.map( [1, 2, [3, 4], [5, 6], 7], function(n){ return n; }); Returns [1, 2, 3, 4, 5, 6, 7] 回答2: Use the power of JavaScript: var a = [[1, 2], 3, [4, 5]]; console.log( Array.prototype.concat.apply([], a) ); //will output [1, 2, 3, 4, 5] 回答3: Here's how you could use jquery to flatten deeply

How to flatten a list to a list without coercion?

徘徊边缘 提交于 2019-11-26 16:09:25
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 function in standard R library which achieves this, or at least some other function which can be used to easily and

How to deserialize JSON into flat, Map-like structure?

空扰寡人 提交于 2019-11-26 16:08:23
问题 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": "defaultHttp", "Enabled": "true", "Number": "10092", "Protocol": "http", "KeepAliveTimeout": "20000", "ThreadPool": { "@enabled": "false", "Max": "150", "ThreadPriority": "5" }, "ExtendedProperties": { "Property": [ { "@name": "connectionTimeout", "$": "20000" } ] } } } Should be deserialized into Map-like