nested-lists

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

本小妞迷上赌 提交于 2019-12-27 11:26:31
问题 This question already has answers here : List of lists changes reflected across sublists unexpectedly (13 answers) Nested List Indices [duplicate] (2 answers) Closed 6 years ago . 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

Function changes list values and not variable values in Python [duplicate]

风格不统一 提交于 2019-12-27 10:27:46
问题 This question already has answers here : Why can a function modify some arguments as perceived by the caller, but not others? (10 answers) Closed 9 months ago . 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 plusOne2(a), a Values of 'y' change but value 'a' stays the same. I have already learned that it's because one is mutable and the other is not. But how to change the code so

NullReference when Adding to nested list

放肆的年华 提交于 2019-12-25 17:13:12
问题 I have class public class Gallery { public string method { get; set; } public List<List<object>> gidlist { get; set; } public int @namespace { get; set; } } Button code private void button1_Click(object sender, EventArgs e) { List<object> data = new List<object>(); data.Add(618395); data.Add("0439fa3666"); Gallery jak = new Gallery(); jak.method = "gdata"; jak.gidlist.Add(data); jak.@namespace = 1; string json = JsonConvert.SerializeObject(jak); textBox2.Text = json; } Here I get System

NullReference when Adding to nested list

半世苍凉 提交于 2019-12-25 17:12:11
问题 I have class public class Gallery { public string method { get; set; } public List<List<object>> gidlist { get; set; } public int @namespace { get; set; } } Button code private void button1_Click(object sender, EventArgs e) { List<object> data = new List<object>(); data.Add(618395); data.Add("0439fa3666"); Gallery jak = new Gallery(); jak.method = "gdata"; jak.gidlist.Add(data); jak.@namespace = 1; string json = JsonConvert.SerializeObject(jak); textBox2.Text = json; } Here I get System

Accessing pandas DataFrame as a nested list

时光怂恿深爱的人放手 提交于 2019-12-25 09:43:12
问题 I've used the .set_index() function to set the first column as my index to the rows in my dataframe: >>> import pandas as pd >>> df = pd.DataFrame([['x', 1,2,3,4,5], ['y', 6,7,8,9,10], ['z', 11,12,13,14,15]]) >>> df.columns = ['index', 'a', 'b', 'c', 'd', 'e'] >>> df = df.set_index(['index']) >>> df a b c d e index x 1 2 3 4 5 y 6 7 8 9 10 z 11 12 13 14 15 How should the dataframe be manipulated such that I can be accessed like a nested list? e.g. are the following possible: >>> df['x'] [1, 2

Convert Triple Level List Structure into data.frame

时光毁灭记忆、已成空白 提交于 2019-12-25 04:54:15
问题 I have a triple level list, the dput is below. How can I convert this into a data.frame? Output should be a 6x23 data.frame and preferably with the correct column names. The number of rows is the length() of the top level list and the number of columns is combined length() of the lower-most level lists. I tried doing this as suggested here (R list to data frame): df <- data.frame(matrix(unlist(l), nrow=6, byrow=T)) and it almost works, but everything is converted to a factor. This is because

Organising and sorting data from a text file

白昼怎懂夜的黑 提交于 2019-12-25 03:49:29
问题 I've got some information stored in a text file based on people taking a test and the scores they have received. Every time they take the test again, a new score is added. The text file looks like this with the data stored within it: Mike 5 7 9 Terry 6 6 9 Paul 4 5 6 I'd like to be able to retrieve the information from the text file and identify the highest score for each person so that it printed out their name and a single number. If I retrieve the data from the file and store it as a list

Most efficient way to get indexposition of a sublist in a nested list

好久不见. 提交于 2019-12-25 03:16:40
问题 I want to get the indexposition of a sublist in a nested list, e.g 0 for [1,2] in nested_ls = [[1,2],[3,4]]. What's the most elegant way of getting the index of the sublist, is there something similar to index() ? 回答1: Yes. It's called index . >>> [[1, 2], [3, 4]].index([1, 2]) 0 >>> [[1, 2], [3, 4]].index([3, 4]) 1 >>> [[1, 2], [3, 4]].index([1, 5]) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: [1, 5] is not in list 来源: https://stackoverflow.com/questions

How to declare an object that is a list of lists of a generic type [closed]

孤街醉人 提交于 2019-12-25 03:07:35
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . Is it possible to declare an object that is is a list of lists of a generic types . So a little like private List<List<T>> outerList = new List<List<T>>(); but a statement that actually compiles. I do not want to use dynamics like so: private List<List<dynamic>> list2 = new List<List<dynamic>>();

How to declare an object that is a list of lists of a generic type [closed]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 03:07:13
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . Is it possible to declare an object that is is a list of lists of a generic types . So a little like private List<List<T>> outerList = new List<List<T>>(); but a statement that actually compiles. I do not want to use dynamics like so: private List<List<dynamic>> list2 = new List<List<dynamic>>();