nested-lists

Return a list of all variable names in a python nested dict/json document in dot notation

你离开我真会死。 提交于 2019-12-04 12:29:36
I'm looking for a function that operates on a python arbitrarily nested dict/array in JSON-esque format and returns a list of strings keying all the variable names it contains, to infinite depth. So, if the object is... x = { 'a': 'meow', 'b': { 'c': 'asd' }, 'd': [ { "e": "stuff", "f": 1 }, { "e": "more stuff", "f": 2 } ] } mylist = f(x) would return... >>> mylist ['a', 'b', 'b.c', 'd[0].e', 'd[0].f', 'd[1].e', 'd[1].f'] def dot_notation(obj, prefix=''): if isinstance(obj, dict): if prefix: prefix += '.' for k, v in obj.items(): for res in dot_notation(v, prefix+str(k)): yield res elif

Linq on a nested List - select all Id's

流过昼夜 提交于 2019-12-04 10:18:01
问题 I have a nested list, something like this: List<Hotel> Hotels; public class Hotel { List<RoomType> RoomType; } public class RoomType { Room Room; } public class Room { int RoomId; } It's a little convoluted, sorry couldn't think of a better mockup model. The Idea is that I have many hotels, each hotels has many room types, and assume each room type has exactly one room object. Now from Hotels list, I just want to select all RoomId 's.. I am stuck here, while trying to nest all list.. right

Appending to a nested list

删除回忆录丶 提交于 2019-12-04 06:58:52
问题 I'm using a nested list to look up values in a dictionary I created. I then want to append the values found to a list. The problem I don't know how to code is how to I keep the values appended within the same nested list structure? Here's the code where the last line I'm appending the values to an empty list. #Creating a dictionary of FID: LU_Codes from external txt file import sys, arcpy, string, csv text_file = open("H:\SWAT\NC\FID_Whole.txt", "r") Lines = text_file.readlines() text_file

How to sort nested lists into seperate lists with unique values in python?

送分小仙女□ 提交于 2019-12-04 05:32:02
问题 I have two variables: unique_val = [1,2,3] nested_list = [['name1',1],['name2',1],['name3',3],['name4',2],['name5',2],['name6',3]] Basically I want separate lists of the names at each unique value. I struggled to put together a set of nested for loops to no avail. Ideally the output would be something like this: list_1 = ['name1','name2'] list_2 = ['name4','name5'] list_3 = ['name3',name6'] 回答1: Creating variables for each item in unique_val is not a good idea. Instead of hard coding

How to group set of data.frame objects in nested list with different order?

若如初见. 提交于 2019-12-04 05:23:34
问题 I have set of data.frame object in nested list, I want to group them by name of data.frame object. Because each nested list, data.frame objects are placed in different order, I have difficulty to group them in new list. I tried transpose method from purr packages in CRAN, but it wasn't right answer that I expected. Does anyone knows any trick of doing this sort of grouping for data.frame object more efficiently? Thanks a lot example: res_1 <- list(con=list(a.con_1=airquality[1:4,], b.con_1

Plotting list of lists in a same graph in Python

只谈情不闲聊 提交于 2019-12-04 05:20:21
I am trying to plot (x,y) where as y = [[1,2,3],[4,5,6],[7,8,9]] . Say, len(x) = len(y[1]) = len(y[2]) .. The length of the y is decided by the User input. I want to plot multiple plots of y in the same graph i.e, (x, y[1],y[2],y[3],...) . When I tried using loop it says dimension error . I also tried: plt.plot(x,y[i] for i in range(1,len(y))) How do I plot ? Please help. for i in range(1,len(y)): plt.plot(x,y[i],label = 'id %s'%i) plt.legend() plt.show() 来源: https://stackoverflow.com/questions/40073322/plotting-list-of-lists-in-a-same-graph-in-python

Replace a string in list of lists

匆匆过客 提交于 2019-12-04 03:37:49
I have a list of lists of strings like: example = [["string 1", "a\r\ntest string:"],["string 1", "test 2: another\r\ntest string"]] I'd like to replace the "\r\n" with a space (and strip off the ":" at the end for all the strings). For a normal list I would use list comprehension to strip or replace an item like example = [x.replace('\r\n','') for x in example] or even a lambda function map(lambda x: str.replace(x, '\r\n', ''),example) but I can't get it to work for a nested list. Any suggestions? Well, think about what your original code is doing: example = [x.replace('\r\n','') for x in

Python - sort a list of nested lists

*爱你&永不变心* 提交于 2019-12-04 00:19:05
I have input consisting of a list of nested lists like this: l = [[[[[39]]]], [1, 2, 3], [4, [5, 3], 1], [[[[8, 9], 10], 11], 12]] I want to sort this list based on the sum of all the numbers in the nested lists... so, the values I want to sort by of l would look like this: [39, 6, 13, 50] Then I want to sort based on these. So the output should be: [[1, 2, 3], [4, [5, 3], 1], [[[[39]]]], [[[[8, 9], 10], 11], 12]] What's a nice pythonic way of doing this? Alex Coventry A slight simplification and generalization to the answers provided so far, using a recent addition to python's syntax: >>> l =

Use … to modify a nested list within a functional

徘徊边缘 提交于 2019-12-03 11:26:38
In R, I'm trying to create a way to transform function parameters given in ... to values in a pre-determined list within a closure function. I would like to be able to do something like this: function_generator <- function(args_list = list(a = "a", b = "b", c = list(d = "d", e = "e")){ g <- function(...){ ## ... will have same names as args list ## e.g. a = "changed_a", d = "changed_d" ## if absent, then args_list stays the same e.g. b="b", e="e" arguments <- list(...) modified_args_list <- amazing_function(arguments, args_list) return(modified_args_list) } } args_list will be different each

Convert Hierarchical DataTable to Json

瘦欲@ 提交于 2019-12-03 09:01:16
I have a hierarchial data table as follows which generates menu and its sub menus. main menu has parentId as 0. Submenu has parent Ids referring to parentId. ResourceId DisplayName ParentId Url ----------------------------------------------- 1 Home 0 Some Url 2 Student 0 Some Url 3 Staff 0 Some Url 4 Library 0 Some Url 6 StudentAtt 1 Some Url 7 TimeTable 1 Some Url 8 Staff Att 2 Some Url 9 Book Issue 3 Some Url 10 Book Return 3 Some Url 11 Fee Payment 4 Some Url 12 Book fine 10 Some Url need to convert it to Json. Below is the code i tried out. I am trying to check if ParentId of SubMenu