nested-lists

R: get element by name from a nested list

怎甘沉沦 提交于 2019-11-27 08:07:01
问题 I have a nested list like so: smth <- list() smth$a <- list(a1=1, a2=2, a3=3) smth$b <- list(b1=4, b2=5, b3=6) smth$c <- "C" The names of every element in the list are unique. I would like to get an element from such a list merely by name without knowing where it is located. Example: getByName(smth, "c") = "C" getByName(smth, "b2") = 5 Also I don't really want to use unlist since the real list has a lot of heavy elements in it. 回答1: The best solution so far is the following: rmatch <-

How to directly select the same column from all nested lists within a list?

依然范特西╮ 提交于 2019-11-27 06:49:32
is it possible to directly select a column of all nested lists within a list? My list is created using aggregate() with table(): AgN=aggregate(data,by=list(d$date),FUN=table,useNA="no") AgN$x looks like: $`0` 1 2 3 9 11 0.447204969 0.438509317 0.096894410 0.009937888 0.007453416 $`1` 1 2 4 8 11 0.489974937 0.389724311 0.102756892 0.006265664 0.011278195 … $n I want to get a vector of a specific column of each table, e.g. a vector containing the values of all columns named "1". I am still a R beginner, but even after searching and trying for a long time I found no nice solution. If I want to

Mapping a nested list with List Comprehension in Python?

半腔热情 提交于 2019-11-27 06:47:24
问题 I have the following code which I use to map a nested list in Python to produce a list with the same structure. >>> nested_list = [['Hello', 'World'], ['Goodbye', 'World']] >>> [map(str.upper, x) for x in nested_list] [['HELLO', 'WORLD'], ['GOODBYE', 'WORLD']] Can this be done with list comprehension alone (without using the map function)? 回答1: For nested lists you can use nested list comprehensions: nested_list = [[s.upper() for s in xs] for xs in nested_list] Personally I find map to be

CSS Nested lists items and alternate background

坚强是说给别人听的谎言 提交于 2019-11-27 06:23:00
问题 I am searching for a way to have list items have alternating background colors. When there is a nested list the items keep alternating but the child is indented without having the background color of the parent flow down to its nested children. It is not possible to apply classes. Also the amount of items is variable. Preferably it should work for an infinite amount of nested lists. But if that is not possible a cap on 3 depths (as in picture) should be enough. If it is easier to do by using

iterate python nested lists efficiently

浪子不回头ぞ 提交于 2019-11-27 06:20:40
问题 I am working on a network traffic monitor project in Python. Not that familiar with Python, so I am seeking help here. In short, I am checking both in and out traffic, I wrote it this way: for iter in ('in','out'): netdata = myhttp() print data netdata is a list consisting of nested lists, its format is like this: [ [t1,f1], [t2,f2], ...] Here t represents the moment and f is the flow. However I just want to keep these f at this moment for both in and out, I wonder any way to get an efficient

How can I convert XHTML nested list to pdf with iText?

◇◆丶佛笑我妖孽 提交于 2019-11-27 05:30:32
I have XHTML content, and I have to create from this content a PDF file on the fly. I use iText pdf converter. I tried the simple way, but I always get bad result after calling the XMLWorkerHelper parser. XHTML: <ul> <li>First <ol> <li>Second</li> <li>Second</li> </ol> </li> <li>First</li> </ul> The expected value: First Second Second First PDF result: First Second Second First In the result there is no nested list. I need a solution for calling the parser, and not creating an iText Document instance. Please take a look at the example NestedListHtml In this example, I take your code snippet

Replace list of list with “condensed” list of list while maintaining order

馋奶兔 提交于 2019-11-27 04:30:35
I have a list of list as in the code I attached. I want to link each sub list if there are any common values. I then want to replace the list of list with a condensed list of list. Examples: if I have a list [[1,2,3],[3,4]] I want [1,2,3,4] . If I have [[4,3],[1,2,3]] I want [4,3,1,2] . If I have [[1,2,3],[a,b],[3,4],[b,c]] I want [[1,2,3,4],[a,b,c]] or [[a,b,c],[1,2,3,4]] I don't care which one. I am almost there... My problem is when I have a case like [[1,2,3],[10,5],[3,8,5]] I want [1,2,3,10,5,8] but with my current code I get [1,2,3,8,10,5] Here is my code: import itertools a = [1,2,3] b

How to convert nested list of lists into a list of tuples in python 3.3?

非 Y 不嫁゛ 提交于 2019-11-27 04:29:25
问题 I am trying to convert a nested list of lists into a list of tuples in Python 3.3. However, it seems that I don't have the logic to do that. The input looks as below: >>> nested_lst = [['tom', 'cat'], ['jerry', 'mouse'], ['spark', 'dog']] And the desired ouptput should look as exactly as follows: nested_lst_of_tuples = [('tom', 'cat'), ('jerry', 'mouse'), ('spark', 'dog')] 回答1: Just use a list comprehension: nested_lst_of_tuples = [tuple(l) for l in nested_lst] Demo: >>> nested_lst = [['tom',

Convert python decimal to string in deeply nested and unpredictable list

萝らか妹 提交于 2019-11-26 21:57:45
问题 I am trying to loop through every value in a deeply nested/mixed list and convert any Decimal instances to string so that I can store them in mongo. My attempt at recursion reached the max depth. I would like to solve this iteratively or in a performant manner. This doesn't seem to work but is my latest attempt: def convert_decimals(root_obj): objs_to_convert = [root_obj] while objs_to_convert: obj = objs_to_convert.pop(0) for k, v in enumerate(obj): if len(v): objs_to_convert.append(v) elif

“Deep copy” nested list without using the deepcopy function

强颜欢笑 提交于 2019-11-26 21:42:00
问题 I am trying to copy the nested list a , but do not know how to do it without using the copy.deepcopy function. a = [[1, 2], [3, 4]] I used: b = a[:] and b = a[:][:] But they all turn out to be shallow copy. Any hints? 回答1: My entry to simulate copy.deepcopy : def deepcopy(obj): if isinstance(obj, dict): return {deepcopy(key): deepcopy(value) for key, value in obj.items()} if hasattr(obj, '__iter__'): return type(obj)(deepcopy(item) for item in obj) return obj The strategy: iterate across each