nested-lists

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

北慕城南 提交于 2019-11-26 10:15:18
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] >>> c.append(c) >>> c [1, 2, 3, [...]] >>> In the last case there's actually an infinite recursion. c[-1] and c are the same. Why is it different with the + operation? Abel To explain "why": The + operation adds the array elements to the original array. The array.append operation inserts the array (or any object) into the end of the original array, which results in a reference to self in that spot (hence the infinite recursion). The

Converting nested list to dataframe

允我心安 提交于 2019-11-26 07:33:46
问题 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 \

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

南笙酒味 提交于 2019-11-26 04:55:53
问题 This question already has answers here : List of lists changes reflected across sublists unexpectedly (13 answers) Closed last year . 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, 0], [0, 9, 0], [0, 9, 0]] well, obviously, what I want is : [[0, 9, 0], [0, 0, 0], [0, 0, 0]] isn\'t strange? I\'m not very familiar with Python, but Python always impresses me with its intuitive behavior.

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

与世无争的帅哥 提交于 2019-11-26 01:51:48
问题 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] >>> c.append(c) >>> c [1, 2, 3, [...]] >>> In the last case there\'s actually an infinite recursion. c[-1] and c are the same. Why is it different with the + operation? 回答1: To explain "why": The + operation adds the array elements to the original array. The array.append operation inserts the array (or any object) into the end of the

Proper way to make HTML nested list?

自作多情 提交于 2019-11-25 23:04:43
问题 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 these ways is the correct way to write an HTML list? Option 1 : the nested <ul> is a child of the parent <ul> <ul> <li>List item one</li> <li>List item two with subitems:</li> <ul> <li>Subitem 1</li> <li>Subitem 2</li> </ul> <li>Final list item</li> </ul> Option 2 : the nested <ul> is a child of

List of lists changes reflected across sublists unexpectedly

耗尽温柔 提交于 2019-11-25 22:09:27
问题 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 the innermost values: myList[0][0] = 5 Now my list looks like this: [[5, 1, 1, 1], [5, 1, 1, 1], [5, 1, 1, 1]] which is not what I wanted or expected. Can someone please explain what\'s going on, and how to get around it? 回答1: When you write [x]*3 you get, essentially, the list [x, x, x] . That is, a list