nested-lists

How to create a number of empty nested lists in python [duplicate]

微笑、不失礼 提交于 2019-11-26 20:48:31
问题 This question already has an answer here: List of lists changes reflected across sublists unexpectedly 13 answers I want to have a variable that is a nested list of a number of empty lists that I can fill in later. Something that looks like: my_variable=[[], [], [], []] However, I do not know beforehand how many lists I will need, only at the creation step, therefore I need a variable a to determine it. I thought about simple my_variable=[[]]*a , but that creates copies of lists and it is not

Converting nested list to dataframe

时间秒杀一切 提交于 2019-11-26 20:17:34
The goal is to convert a nested list which sometimes contain missing records into a data frame. An example of the structure when there are missing records is: str(mylist) List of 3 $ :List of 7 ..$ Hit : chr "True" ..$ Project: chr "Blue" ..$ Year : chr "2011" ..$ Rating : chr "4" ..$ Launch : chr "26 Jan 2012" ..$ ID : chr "19" ..$ Dept : chr "1, 2, 4" $ :List of 2 ..$ Hit : chr "False" ..$ Error: chr "Record not found" $ :List of 7 ..$ Hit : chr "True" ..$ Project: chr "Green" ..$ Year : chr "2004" ..$ Rating : chr "8" ..$ Launch : chr "29 Feb 2004" ..$ ID : chr "183" ..$ Dept : chr "6, 8"

How does the min/max function on a nested list work?

我是研究僧i 提交于 2019-11-26 20:09:48
问题 Lets say, there is a nested list, like: my_list = [[1, 2, 21], [1, 3], [1, 2]] When the function min() is called on this: min(my_list) The output received is [1, 2] Why and How does it work? What are some use cases of it? 回答1: How are lists and other sequences compared in Python? Lists (and other sequences) in Python are compared lexicographically and not based on any other parameter. Sequence objects may be compared to other objects with the same sequence type. The comparison uses

Python: Return 2 ints for index in 2D lists given item

[亡魂溺海] 提交于 2019-11-26 18:34:38
问题 I've been tinkering in python this week and I got stuck on something. If I had a 2D list like this: myList = [[1,2],[3,4],[5,6]] and I did this >>>myList.index([3,4]) it would return 1 However, I want the index of something in side one of the lists, like this >>>myList.index(3) and it would return 1, 0 Is there anything that can do this? Cheers 回答1: Try this: def index_2d(myList, v): for i, x in enumerate(myList): if v in x: return (i, x.index(v)) Usage: >>> index_2d(myList, 3) (1, 0) 回答2: If

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

自作多情 提交于 2019-11-26 17:32:48
问题 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

How to use <ui:repeat> to iterate over a nested list?

独自空忆成欢 提交于 2019-11-26 17:24:35
问题 Using JSF 2.0, I need to display a table wherein each row contains a link which opens a popup. I have two models: A which has id and List<B> properties and B which has id and name properties. In my backing bean, I have a List<A> property. In my view, I am using <ui:repeat> to iterate over List<A> . The requirement is, depending on the row that the user clicks, the corresponding List<B> of A needs to be displayed. However, the <ui:repeat> does not accept a nested list to be assigned in the var

Split a list into nested lists on a value

浪尽此生 提交于 2019-11-26 16:37:59
Say I have a list like so: [1, 4, None, 6, 9, None, 3, 9, 4 ] I decide to split this into nested lists on None , to get this: [ [ 1, 4 ], [ 6, 9 ], [ 3, 9, 4 ] ] Of course, I could have wanted to do this on (9, None) in which case, we would have got: [ [ 1, 4 ], [ 6 ], [ 3 ], [ 4 ] ] This is trivial to do using list append through iteration ( in a for loop ) I am interested to know whether this can be done in something faster - like a list comprehension? If not, why not ( for example, a list comprehension cannot return more than one list element per iteration? ) Kabie >>> def isplit(iterable

Generating sublists using multiplication ( * ) unexpected behavior [duplicate]

时间秒杀一切 提交于 2019-11-26 15:27:10
This question already has an answer here: List of lists changes reflected across sublists unexpectedly 12 answers Nested List Indices [duplicate] 2 answers I'm sure this has been answered somewhere but I wasn't sure how to describe it. Let's say I want to create a list containing 3 empty lists, like so: lst = [[], [], []] I thought I was being all clever by doing this: lst = [[]] * 3 But I discovered, after debugging some weird behavior, that this caused an append update to one sublist, say lst[0].append(3) , to update the entire list, making it [[3], [3], [3]] rather than [[3], [], []] .

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

Deadly 提交于 2019-11-26 12:56:28
问题 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

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

百般思念 提交于 2019-11-26 12:49:09
问题 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