nested-lists

Python class that works as list of lists

我的梦境 提交于 2019-12-13 09:54:35
问题 I'm trying to create a python class that can work as a list of lists. However, all I've managed to develop so far is, class MyNestedList(list): ... I'm aware that the above code will work as, my = MyNestedList() my[0] = 1 ... But I want my class to work as, my[0][0] = 1 ... Will anyone please guide me further? EDIT: I want the class to pass as a type for the deap framework, as my individual. I can't pass list of lists as my type as it would break my structure. 回答1: Here is an example. You

mongodb aggregate to get length of first nested array/list

↘锁芯ラ 提交于 2019-12-13 08:56:39
问题 I have following example { username : 'Alex', tags: ['C#', 'Java', 'C++'] } To get the length of tags array db.users.aggregate( [ { $project: { tags_count: {$size: "$tags"} } } ] ) But how to get the length when I have nested lists { username : 'Alex', tags: [['C#', 'Java', 'C++']] } res = collection.aggregate([{ "$match": query },{ "$project": { "_id": 0, "name": "$name", "a1":{"$properties.has_a":{"$slice":1}}, //Line #a1 } }]) I also tried changing Line #a1 as follows too: res = collection

Convert Nested List into data.frame with different column length

拟墨画扇 提交于 2019-12-13 08:07:46
问题 I was trying to convert below nested list into data.frame but without luck. There are a few complications, mainly the column "results" of position 1 is inconsistent with position 2, as there is no result in position 2. item length inconsistent across different positions [[1]] [[1]]$html_attributions list() [[1]]$results geometry.location.lat geometry.location.lng 1 25.66544 -100.4354 id place_id 1 6ce0a030663144c8e992cbce51eb00479ef7db89 ChIJVy7b7FW9YoYRdaH2I_gOJIk reference 1

Sorting elements from a nested based on the second element in each nested list? [duplicate]

非 Y 不嫁゛ 提交于 2019-12-13 07:52:36
问题 This question already has answers here : How to sort nested lists into seperate lists with unique values in python? (2 answers) Closed 6 years ago . So I have a nested list that contains words and numbers like the following example: nested_list = [['This', 1],['is' , 2],['a', 3],['list', 4]] I also have a list of numbers: number_list = [2,3] I want to generate a two nested lists based on weather the second element of the list contains a number in the list of numbers. I want to output to be:

Append lists for CSV output in Python

时光总嘲笑我的痴心妄想 提交于 2019-12-13 06:02:21
问题 At the moment I am scraping data from the web and want to output it into CSV. Everything is working fine but as soon as I append more than one list in the iteration the list has the wrong format. I start with sth like this: list = [a, b, c] list_two = [d, e, f] list_three = [g, h, i] first iteration: list = [list, list_two] # list = [[a, b, c], [d, e, f]] second iteration: list = [list, list_three] I get: # list = [[[a, b, c], [d, e, f]], [g, h, i]] I want to have: # list = [[a, b, c], [d, e,

turn lists of lists into strings pandas dataframe

落爺英雄遲暮 提交于 2019-12-12 18:52:18
问题 Background I have the following toy df that contains lists in the columns Before and After as seen below import pandas as pd before = [list(['in', 'the', 'bright', 'blue', 'box']), list(['because','they','go','really','fast']), list(['to','ride','and','have','fun'])] after = [list(['there', 'are', 'many', 'different']), list(['i','like','a','lot','of', 'sports']), list(['the','middle','east','has','many'])] df= pd.DataFrame({'Before' : before, 'After' : after, 'P_ID': [1,2,3], 'Word' : [

Dictionary of nested lists to pandas DataFrame

喜你入骨 提交于 2019-12-12 18:18:26
问题 I'm trying to learn how pandas works but I assume I'm missing something obvious. I have a file looking like this : dict_spl ={'doc1':[[('word11',1,1),('word12',1,2)]], 'doc2':[[('word21',2,1),('word22',2,2)]]} And I'm trying to obtain a pandas DataFrame looking like this: # doc1 word11 1 1 # doc1 word12 1 2 # doc2 word21 2 1 # doc2 word22 2 2 I haven't found a way to create both new columns and new rows while duplicating the common values. 回答1: You can use: a = [[(k, *y) for y in v[0]] for k

Python unpack 2-dimensional list of named tuples

老子叫甜甜 提交于 2019-12-12 18:09:07
问题 I have a 2-dimensional list of named tuples (let's say that each tuple has N values), and I want to unpack them into N different 2-dimensional lists where each unpacked 2-D list is composed entirely of a single attribute from the original list. For example if I have this 2-D list: >>> combo = namedtuple('combo', 'i, f, s') >>> combo_mat = [[combo(i + 3*j, float(i + 3*j), str(i + 3*j)) for i in range(3)] for j in range(3)] >>> combo_mat [[combo(i=0, f=0.0, s='0'), combo(i=1, f=1.0, s='1'),

append the same element to several sublists in python

折月煮酒 提交于 2019-12-12 17:56:15
问题 I have a list of lists like this: L=[[[1,2,3],[4,5]],[[6,7,8,9],[10]]] I want to append the integer 11 to the subsublists 1 and 3. I can do something like: L[0][2].append(11) L[1][2].append(11) Is there a simpler way to do it in Python ? Because in my case, let's say I have a list with 100 sublists, and these sublists have 100 sublists (comparable to a (100,100)-matrix) and I want to append a value to the sublists from nb 50 to 75 of the sublists from nb 10 to 20. So right now I do something

How to update the Nested List/Tree Store in Sencha Touch?

女生的网名这么多〃 提交于 2019-12-12 17:18:47
问题 I have a nested list which must be filled with new data based on what does user select in an Ext.Carousel. TreeStore.load(newData) // this does not work :( TreeStore.removeAll() // this works! It seems that the docs and the forum don't have the answer, cause I have been searching for 2-3 days. Thank you in advance. 回答1: I've ended up with following solution: NestedList = Ext.extend(Ext.NestedList, { loadData: function(data) { this.store.setProxy({ type:'memory', data: data, reader: { type: