All Questions

344
votes
13answers
15068 views

List of lists changes reflected across sublists unexpectedly

I needed to create a list of lists in Python, so I typed the following: myList = [[1] * 4] * 3 The list looked like this: [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]] Then I changed one of t...
392
votes
7answers
227157 views

Proper way to make HTML nested list?

The W3 docs have a nested list example prefixed by DEPRECATED EXAMPLE:, but they never corrected it with a non-deprecated example, nor explained exactly what is wrong with the example. So which of th...
68
votes
7answers
67946 views

Python append() vs. + operator on lists, why do these give different results?

Why do these two operations (append() resp. +) give different results? >>> c = [1, 2, 3] >>> c [1, 2, 3] >>> c += c >>> c [1, 2, 3, 1, 2, 3] >>> c = [1, 2...
3
votes
2answers
838 views

why can't I change only a single element in a nested list in Python

I just met something really strange of Python: >>> out=[[0]*3]*3 >>> out [[0, 0, 0], [0, 0, 0], [0, 0, 0]] >>> out[0][1] 0 >>> out[0][1]=9 >>> out [[0, 9,...
11
votes
5answers
1197 views

Generating sublists using multiplication ( * ) unexpected behavior

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 cl...
22
votes
5answers
11040 views

Split a list into nested lists on a value

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...
4
votes
6answers
10889 views

Function changes list values and not variable values in Python

Let's take a simple code: y = [1,2,3] def plusOne(y): for x in range(len(y)): y[x] += 1 return y print plusOne(y), y a = 2 def plusOne2(a): a += 1 return a print plusOne...
8
votes
3answers
14421 views

Converting nested list to dataframe

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 ...
21
votes
7answers
1283 views

Replace list of list with "condensed" list of list while maintaining order

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 ha...
3
votes
1answers
923 views

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

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 par...

Previous Next