nested-lists

How to convert a nested list into a one-dimensional list in Python?

二次信任 提交于 2021-02-17 13:00:26
问题 I tried everything (in my knowledge) from splitting the array and joining them up together and even using itertools: import itertools def oneDArray(x): return list(itertools.chain(*x)) The result I want: a) print oneDArray([1,[2,2,2],4]) == [1,2,2,2,4] Strangely, it works for b) print oneDArray([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) == [1, 2, 3, 4, 5, 6, 7, 8, 9] Question 1) How do I get part a to work the way I want (any hints?) Question 2) Why does the following code above work for part b and

Extract elements by name from a nested list

感情迁移 提交于 2021-02-16 18:14:49
问题 For a named, nested list, what is the best way to extract a specific element? If I have a list with known fields (eg, from a yaml file), I want to extract an element (list or otherwise) without having to search through the names and indices or trying to keep track of the levels in the str output. For example, I know that lm returns a nested list which contains qr info. fit <- lm(mpg ~ wt, mtcars) fit$qr$qraux # [1] 1.176777 1.046354 But if I don't know the order, I just want to specify the

Boost::Multi-index for nested lists

不想你离开。 提交于 2021-02-11 13:10:13
问题 How to implement Boost::Multi-index on a list of lists I have a hierarchical tree as follows: typedef std::list<struct obj> objList // the object list typedef std::list<objList> topLevelList // the list of top-level object lists struct obj { int Id; // globally unique Id std::string objType; std::string objAttributes; .... topLevelList childObjectlist; } At the top-level, I have a std::list of struct obj Then, each of these top-level obj can have any number of child objects, which are

Create the following list [1, 2, [3, 4], [3, [4, 5]]] in Haskell

亡梦爱人 提交于 2021-02-10 03:10:36
问题 I'm trying to make a list that contains sub lists, like [1, 2, [3, 4], [3, [4, 5]]] . It seems like I should define a new type. I tried: data NestedList a = Int a | List [NestedList a] but I think its wrong, or that I don't know how to use it. I'm very new to Haskell and I'm not sure about the meaning of this expression. Does it mean that it is making a "type" Int with parameter a and a "type" List with parameter [NestedList a] ? The data expression above was taken from the solution to the

Square of Numbers in Nested List Python 3

别说谁变了你拦得住时间么 提交于 2021-02-08 13:49:33
问题 The problem I have to solve is one that takes a nested list as an input, and returns the same nested list, except each element is the square of the element that previously existed in that spot. This is my code >>> def treemap(lst): ... for element in lst: ... if element == type(list): ... return treemap(element) ... else: ... element=element**2 ... return lst >>> lst = [1, 2, 3, [4, [5, 6], 7]] >>> print(treemap(lst)) Now I am getting an error that is saying 'int' object is not iterable. I'm

How to efficiently extract values from nested numpy arrays generated by loadmat function?

为君一笑 提交于 2021-02-07 19:13:49
问题 Is there a more efficient method in python to extract data from a nested python list such as A = array([[array([[12000000]])]], dtype=object) . I have been using A[0][0][0][0] , it does not seem to be an efficinet method when you have lots of data like A. I have also used numpy.squeeeze(array([[array([[12000000]])]], dtype=object)) but this gives me array(array([[12000000]]), dtype=object) PS: The nested array was generated by loadmat() function in scipy module to load a .mat file which

How to efficiently extract values from nested numpy arrays generated by loadmat function?

让人想犯罪 __ 提交于 2021-02-07 19:12:11
问题 Is there a more efficient method in python to extract data from a nested python list such as A = array([[array([[12000000]])]], dtype=object) . I have been using A[0][0][0][0] , it does not seem to be an efficinet method when you have lots of data like A. I have also used numpy.squeeeze(array([[array([[12000000]])]], dtype=object)) but this gives me array(array([[12000000]]), dtype=object) PS: The nested array was generated by loadmat() function in scipy module to load a .mat file which

How do I split strings within nested lists in Python?

自古美人都是妖i 提交于 2021-02-04 06:22:02
问题 I know how to split a list of strings into a nested list using those strings, but I'm not specifically sure how I would go about splitting those strings now into multiple strings. For example: def inputSplit(file_name): with open(file_name) as f: content = f.read().splitlines() i = 0 contentLists = [content[i:i+1] for i in range(0, len(content), 1)] Would give me something like: [['these are some words'], ['these are some more words'], ['these are even more words'], ['these are the last words

How do I split strings within nested lists in Python?

岁酱吖の 提交于 2021-02-04 06:21:27
问题 I know how to split a list of strings into a nested list using those strings, but I'm not specifically sure how I would go about splitting those strings now into multiple strings. For example: def inputSplit(file_name): with open(file_name) as f: content = f.read().splitlines() i = 0 contentLists = [content[i:i+1] for i in range(0, len(content), 1)] Would give me something like: [['these are some words'], ['these are some more words'], ['these are even more words'], ['these are the last words

How to combine multiple columns of an r data frame into a single column that is a list

為{幸葍}努か 提交于 2021-01-28 13:32:28
问题 I would like to combine multiple columns that I have in a data frame into one column in that data frame that is a list. For example, I have the following data frame ingredients: name1 name2 imgID attr1 attr2 attr3... Item1 ItemID1 Img1 water chocolate soy... Item2 ItemID2 Img2 cocoa spice milk... I would like to combine the attr columns into one column that is a comma-separated list of those items and if possible have them appear in the following format: name1 name2 imgID attrs Item1 ItemID1