nested-lists

How to find elements that are common to all lists in a nested list?

烈酒焚心 提交于 2019-11-29 16:08:07
I have a large nested list and each list within the nested list contains a list of numbers that are formatted as floats. However every individual list in the nested list is the same except for a few exceptions. I want to extract the numbers that are common to all of the lists in the nested list. A simple example of my problem is shown below: nested_list = [[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0], [2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0], [1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0], [2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0

iterate python nested lists efficiently

扶醉桌前 提交于 2019-11-29 10:46:46
I am working on a network traffic monitor project in Python. Not that familiar with Python, so I am seeking help here. In short, I am checking both in and out traffic, I wrote it this way: for iter in ('in','out'): netdata = myhttp() print data netdata is a list consisting of nested lists, its format is like this: [ [t1,f1], [t2,f2], ...] Here t represents the moment and f is the flow. However I just want to keep these f at this moment for both in and out, I wonder any way to get an efficient code. After some search, I think I need to use create a list of traffic(2 elements), then use zip

Nested List, Multiple Layouts

我怕爱的太早我们不能终老 提交于 2019-11-29 08:20:47
In Sencha Touch 2.1, I have the following nested list defined: xtype: 'NestedList', docked: 'top', ui: 'light', store: treeStore, detailCard: true, detailContainer: // Reference to a Another Panel I can get the Nested List to appear, but adding items via JSON is proving problematic. Here's a sample of my JSON: [ { "BranchID" : 4, "BranchName" : "Branch Name", "Jobs" : [ { "JobOrderID" : 75, "JobTitle" : "Job Title", "leaf" : true } ] } ] And here is my Tree Store and List Item: // Define a List Item: Ext.define('Branch', { extend: 'Ext.data.Model', config: { fields: [ 'BranchID', 'BranchName'

Adding data to a nested list in Python

故事扮演 提交于 2019-11-29 04:54:56
I have a nested list e.g.: nlist = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], ] Before I insert this list into a database, I would like to add a "column" to it with the same value in each row of the new column e.g: nlist = [ [a, 1, 2, 3], [a, 4, 5, 6], [a, 7, 8, 9], ] What's the best way to do this, when, for example, the original nested list might have hundreds of rows? Why not change the original list (if that is all you want to do): for row in nlist: row.insert(0, a) If you are looking to create a new list then this will work as well... nlistnew = [[a]+row for row in nlist] EDIT: Fixed code as per

Print a nested list line by line - Python

孤街醉人 提交于 2019-11-29 02:34:06
A = [[1, 2, 3], [2, 3, 4], [4, 5, 6]] I am trying my best to print A of the form: 1 2 3 2 3 4 4 5 6 That is in different lines, but I am unable to do so without all the elements in different lines. This is my code so far: for r in A: for t in r: print(t,) print This is my output: 1 2 3 2 3 4 4 5 6 It seems really simple, and I think a minor change would do it. Thanks! Use a simple for loop and " ".join() mapping each int in the nested list to a str with map() . Example: >>> ys = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]] >>> for xs in ys: ... print(" ".join(map(str, xs))) ... 1 2 3 4 5 6 7 8 9 10

Print list of lists in separate lines

元气小坏坏 提交于 2019-11-29 01:50:09
问题 I have a list of lists: a = [[1, 3, 4], [2, 5, 7]] I want the output in the following format: 1 3 4 2 5 7 I have tried it the following way , but the outputs are not in the desired way: for i in a: for j in i: print(j, sep=' ') Outputs: 1 3 4 2 5 7 While changing the print call to use end instead: for i in a: for j in i: print(j, end = ' ') Outputs: 1 3 4 2 5 7 Any ideas? 回答1: Iterate through every sub-list in your original list and unpack it in the print call with * : a = [[1, 3, 4], [2, 5,

Most elegant way to modify elements of nested lists in place

眉间皱痕 提交于 2019-11-28 19:12:19
I have a 2D list that looks like this: table = [['donkey', '2', '1', '0'], ['goat', '5', '3', '2']] I want to change the last three elements to integers, but the code below feels very ugly: for row in table: for i in range(len(row)-1): row[i+1] = int(row[i+1]) But I'd rather have something that looks like: for row in table: for col in row[1:]: col = int(col) I think there should be a way to write the code above, but the slice creates an iterator/new list that's separate from the original, so the references don't carry over. Is there some way to get a more Pythonic solution? for row in table:

R: get element by name from a nested list

Deadly 提交于 2019-11-28 14:04:57
I have a nested list like so: smth <- list() smth$a <- list(a1=1, a2=2, a3=3) smth$b <- list(b1=4, b2=5, b3=6) smth$c <- "C" The names of every element in the list are unique. I would like to get an element from such a list merely by name without knowing where it is located. Example: getByName(smth, "c") = "C" getByName(smth, "b2") = 5 Also I don't really want to use unlist since the real list has a lot of heavy elements in it. The best solution so far is the following: rmatch <- function(x, name) { pos <- match(name, names(x)) if (!is.na(pos)) return(x[[pos]]) for (el in x) { if (class(el) ==

How to find elements that are common to all lists in a nested list?

只愿长相守 提交于 2019-11-28 10:03:39
问题 I have a large nested list and each list within the nested list contains a list of numbers that are formatted as floats. However every individual list in the nested list is the same except for a few exceptions. I want to extract the numbers that are common to all of the lists in the nested list. A simple example of my problem is shown below: nested_list = [[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0], [2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0], [1.0,2.0

Nested List, Multiple Layouts

徘徊边缘 提交于 2019-11-28 01:41:11
问题 In Sencha Touch 2.1, I have the following nested list defined: xtype: 'NestedList', docked: 'top', ui: 'light', store: treeStore, detailCard: true, detailContainer: // Reference to a Another Panel I can get the Nested List to appear, but adding items via JSON is proving problematic. Here's a sample of my JSON: [ { "BranchID" : 4, "BranchName" : "Branch Name", "Jobs" : [ { "JobOrderID" : 75, "JobTitle" : "Job Title", "leaf" : true } ] } ] And here is my Tree Store and List Item: // Define a