FAQ Database Discussion Community


Nested foreach loops to get info from nested lists using C#

c#,xml,foreach,nested-loops,nested-lists
An object EmployeeInfoDocument contains a list of groups. Each group holds a list of employees. I am trying to use a nested foreach loop to write the values out to an XML file. It seems that I have the syntax wrong on the inner foreach loop. Visual Studio is giving...

Common elements between two lists of lists (intersection of nested lists) [duplicate]

python,python-2.7,numpy,nested-lists
This question already has an answer here: Get intersecting rows across two 2D numpy arrays 3 answers I have two large lists of points in 2D and I want to find their common sublists, if they have some. Both of the lists are quite large and efficiency is an...

How do you plot a graph consisting of extracted entries from a nested list?

plot,list-comprehension,python-3.4,nested-lists
I have a nested list such that; nested_list = [[4, 3, 0], [6, 8, 7], [3, 1, 8], [2, 1, 3], [9, 9, 3], ...] which has 100 entries. I need to plot a graph of all the first elements of each sub-list where Sub_List_1 = [4, 6, 3, 2,...

nesting a series of lists within a list - python

python,list,nested-lists
Originally this was lists within a list: print results [['aaa664847', 'Completed', 'location', 'mode', '2014-xx-ddT20:00:00.000'] ['aaa665487', 'Completed', 'location', 'mode', '2014-xx-ddT19:00:00.000'] ['aaa661965', 'Completed', 'location', 'mode', '2014-xx-ddT18:00:00.000']] However, I needed to join the elements within the nested list which then prints out like this: print results1 ['aaa664847, Completed, location, mode, 2014-xx-ddT20:00:00.000'] ['aaa665487, Completed,...

How do I select a random element from a 2d list

python,python-2.7,nested-lists
I'm trying to select a random element in a 2D list in python. I'm creating a blackjack game. I'm aware of how long and non-optimized this code is, but I'm new to programming and I want to make mistakes to learn. Here's the initial code that I have set up....

How to add two nested lists in parallel and append result to a new list in python

python,list,python-3.x,nested-lists
I'm trying to add all the elements of two unequal nested lists in parallel and append the result back to another new list, i've written a little hacky code that could add them but there's a lot of things wrong with the code, first i tried to make the pairs...

Python Sum of nested list with str items [duplicate]

python,string,sum,nested-lists
This question already has an answer here: Flatten (an irregular) list of lists in Python 24 answers Given: [[[[[[[[['A']]]]],[[[[['B']]]]],['C'],[[[[['D']]]]]] Or something like that. A complicated nested list. How can I return either: 'ABCD' or ['ABCD'] Recursive function with int is simple enough due to the sum helper function, but...

C# 2D Generic List [closed]

c#,list,nested-lists
I'm working on a talents/skills tree system for my game made with Unity and I've got a class for my 45 talents buttons that looks like this: public class SaveTalentClass { public int talId; public int talCurRank; public int talMaxRank; public SaveTalentClass(int id, int cRank, int mRank) { talId =...

Why does destructuring work for walk but not postwalk in Clojure?

clojure,nested,nested-lists,clojure.walk
I can walk the top-level of the following map using walk in Clojure: (use 'clojure.walk) (walk (fn [[k v]] (println (type k) k v)) identity {:a 1 :b {:c 3}}) Result: clojure.lang.Keyword :b {:c 3} clojure.lang.Keyword :a 1 {} (This works in a very similar way to map) But when...

Lists of lists python recursive permutation

python,recursion,permutation,nested-lists
How do you manipulate a list of list of string in a specific way so that lst = [ [a, b], [c, d, e], [f, a, e] ] # turns into lst = [ [a, b], [b, a], [c, d, e], [c, e, d], [d, c, e],etc...] #there are 14...

SML - Concatenating strings in a list within a list

sml,string-concatenation,nested-lists
So I've got this list of lists of strings: [["#@","**","#@"],["##","*%","##"]] What I want to do is transform each inner list into a single string like this: ["#@**#@","##*%##"] Resulting in a list of strings. I've tried various combinations of map, foldr, and anonymous functions, but I can't for the life of...

Find index of a sublist in a list

python,list,nested-lists
Trying to find the index of a sublists with an element. I’m not sure how to specify the problem exactly (which may be why I’ve overlooked it in a manual), however my problem is thus: list1 = [[1,2],[3,4],[7,8,9]] I want to find the first sub-list in list1 where 7 appears...

Putting a list into a list at each new line

python,list,slice,bioinformatics,nested-lists
I am trying to create a new list (within a list) for each set of kmers For example it should return: [['ATT', 'TTT', 'TTG', 'TGG', 'GGC'], ['TGC', 'GCC', 'CCT', 'CTT', 'TTA'], ['CGG', 'GGT', 'GTA', 'TAT', 'ATC'], ['GAA', 'AAA', 'AAA', 'AAT', 'ATT']] Instead of this: ['ATT', 'TTT', 'TTG', 'TGG', 'GGC', 'TGC',...

Writing sublists in a list of lists to separate text files

python,nested-lists
I’m really new to Python but find myself working on the travelling salesman problem with multiple drivers. Currently I handle the routes as a list of lists but I’m having trouble getting the results out in a suitable .txt format. Each sub-list represents the locations for a driver to visit,...

Cast Nested List to Nested List

c#,list,casting,nested,nested-lists
I know its possible to cast a list of items from one type to another but how do you cast a nested list to nested List . Already tried solutions: List<List<String>> new_list = new List<List<string>>(abc.Cast<List<String>>()); and List<List<String>> new_list = abc.Cast<List<String>>().ToList(); Both of which give the following error: Unable to cast...

Can't use scipy stats function on nested list

python,numpy,statistics,scipy,nested-lists
I've been trying to scipy.mstats.zscore a dataset that is intentionally organized into a nested list, and it gives: TypeError: unsupported operand type(s) for /: 'list' and 'long' which probably suggests that scipy.stats doesn't work for nested lists. What can I do about it? Does a for loop affect the nature...

Is there a way of avoiding so many list(chain(*list_of_list))?

python,list,nested-lists,itertools,chain
If I have a list of list of list of tuples of two strings. I want to flatten it out to a non-nested list of tuples, I could do this: >>> from itertools import chain >>> lst_of_lst_of_lst_of_tuples = [ [[('ab', 'cd'), ('ef', 'gh')], [('ij', 'kl'), ('mn', 'op')]], [[('qr', 'st'), ('uv',...

Find duplicate element in a list of lists [closed]

python,duplicates,nested-lists
I am looking for Python ideas for the following problem. Given a list of lists... [[20, 21, 22], [17, 18, 19, 20], [10, 11, 12, 13]] If there is a duplicate element that is common between any or all the lists, return True. If all of the elements are unique,...

python nested list comprehension string concatenation

python,list-comprehension,nested-lists
I have a list of lists in python looking like this: [['a', 'b'], ['c', 'd']] I want to come up with a string like this: a,b;c,d So the lists should be separated with a ; and the values of the same list should be separated with a , So far...

list comprehension with multiple assignments

python,list,python-2.7,nested-lists
I presently have this code for factoring large numbers: def f1(n): return [[i, n//i] for i in range(1 , int(n**0.5) + 1) if n % i == 0] It's the fastest version I've seen so far (If there's a faster way I'd love to know about that as well), but...

Is it possible to index nested lists using tuples in python?

python,tuples,nested-lists
I just started with python and very soon wondered if indexing a nested list with a tuple was possible. Something like: elements[(1,1)] One example where I wanted to do that was something similar to the code below in which I save some positions of the matrix that I will later...

Findings letters that match in every sublist

python,for-loop,collections,nested-loops,nested-lists
candidates = ['A', 'B', 'C', 'D'] If a candidate appears in every sublist at least once they must be returned listOfData = [['B','C','B','A'], #D is no longer a candidate ['B', 'C', 'B', 'D'], #A is no loner a candidate ['A','D','C','B'], # B and C are still candidates ['D', 'C', 'B',...

Add extra left margin to a singe ListView row

android,listview,nested-lists
I need to graphically simulate a nested ListView with possible n levels. The only thing I need is to add left margin to the "child" based on its level. I think that a could achieve that with a RelativeLayout and a left-blank LinearLayout which can be hidden at level 0...

Index a list of lists

python,indexing,nested-lists
So sorry for bothering you with this probably stupid question, but I have been stuck (once again) on this for a while now. I have list of lists abc = [['date1','number1'],['date2','number2']...] The dates might be the same. For example: date1 and date2 might both be '02/02/2015', while date3 could be...

subtract list of lists from list of lists with conditional arguments

python,for-loop,conditional-statements,nested-lists,subtraction
I have sort of a tricky problem that I'm having a hard time wrapping my head around. I have two lists of lists: firstList = [[0, 9], [0, 4], [0]] secondList = [[18], [19, 7], [20]] I would like to subtract the value(s) in firstList from the values in secondList...

How do I split strings within nested lists in Python?

python,string,list,nested-lists,string-split
I know how to split a list of strings into a nested list using those strings, but I'm not specifically sure how I would go about splitting those strings now into multiple strings. For example: def inputSplit(file_name): with open(file_name) as f: content = f.read().splitlines() i = 0 contentLists = [content[i:i+1]...

sum of first value in nested list

python,sum,nested-lists
In traditional python, the sum function gives the sum of a list: sum([0,1,2,3,4])=10 On the other hand, what if you have a nested list as so: sum([[1,2,3],[4,5,6],[7,8,9]]) We find the error: Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'int' and...

Python, iterating between 2 lists of lists, seeing if values in one list are similar to values in the other and extracting them

python,list,iterator,iteration,nested-lists
I have two list of lists containing cartesian coordinates: LBCoord = [[1195,456],[1324,674],[5644,687],[4679,654]] #very long list CoreCoord = [[1145,466],[1524,644],[5664,657],[4479,654]] #very long list I want to iterate through these two lists, seeing if any of the two sets of coordinates in both x (LBCoord[n][0]) and y (LBCoord[n][1]), fall within a user determined...

Can't create nested list beacuse list.Clear doesn't seem to work as I want

c#,foreach,nested-lists
I try to load txt file that consists of 200 rows each of different length into nested list so that every sublist inside main list will be equal to row, so there will be 200 sublists. class MainClass { public static void Main (string[] args) { List<List<int>> arrayList = new...

Print a nested list line by line - Python

python,python-3.x,for-loop,printing,nested-lists
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....

How do I get values from a nested list in R when the sublists are different sizes and not named?

r,nested-lists
I have a list of n lists, each of which are different lengths. These lists contain a third level list of two character vectors, one which is the name of the value and the other is the value. I want to extract all the values where the same name is...

Django Rest Framework: Deserializing and get the primary key from validated_data

json,django,primary-key,django-rest-framework,nested-lists
I defined a nested model Product as follow. Each Product can belong to a lot of Productlist. class Product(models.Model): product_id = models.AutoField(primary_key=True) product_name = models.CharField(max_length=50) class Productlist(models.Model): productlist_id = models.AutoField(primary_key=True) productlist_name = models.CharField(max_length=50) product = models.ManyToManyField(Product, related_name='productlists') The corresponding serializers are: class ProductlistSerializer(serializers.ModelSerializer): class Meta: model =...

How to print a nested list of objects with the highest sum in python

python,object,python-3.x,nested-lists
I'm still learning python and i'm still having a tough time working with objects, i'm trying to write a program that calculates the critical path of a set of activities in a project, I've been able to get the critical path of the activities and they're stored as a list...

Compare dictionary key, values with nested list elements - Python

python,dictionary,nested-lists
Trying to match key, values in dictionaries with nested list elements dict = {'a':[1, 5], 'c':[7, 9], 'f':[10, 12], 'b':[15, 20]} list_A = [['a', '4'], ['a', '7'], ['b', '17'], ['b', 10], ['c', '7'], ['d', '7'], ['f', '11'], ['f', '12']] list_A_no_reps =['a', 'b', 'c', 'd', 'f'] I am trying to get...

HTML/CSS | Remove Border Right from nested list

html,css,nested-lists
I searched all over StackOverflow but couldn't find an answer to this one. How can I get border-right to affect only the main <li>s (ie: "Border here ->") but not the nested <li>s (ie: "No border ->")? Code: body { margin: 0; padding: 0; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;...

Creating a dictionary in a list from known variables [closed]

python,list,dictionary,nested-lists
I have some known values/variables a,b,c,d. I would like to use these variables to create a dictionary in a list. I did the following but it did not work: nestedlist['Name'] = a nestedlist['Address']['StreetNumber'] = b nestedlist['Address']['Zipcode']=c nestedlist['Email']=d I am getting the following error: Traceback (most recent call last): nestedlist['Address']['StreetNumber'] =...

how to write the contents of a file into lists in python?

python,python-2.7,nested-lists
i am learning python programming and am stuck with this problem.I looked into the other examples which reads the file input and makes the entire thing as a single list or as a string link to that example but i want each line to be a list(nested lists) how do...

Convert list-of-dicts into tree

python,list,tree,category,nested-lists
For two days I try to traverse a list of dicts into a tree. `list_of_dicts = [ {'name':Category1, 'id': '7', 'parent_id': '7', 'level': '1'} {'name':Category3, 'id': '33', 'parent_id': '7', 'level': '2'} {'name':Category5, 'id': '334', 'parent_id': '33', 'level': '3'} {'name':Category10, 'id': '23', 'parent_id': '7', 'level': '2'} {'name':Category2, 'id': '8', 'parent_id': '8',...

User input to store in nested lists with int conversion - Python

python,int,append,nested-lists
I need user input to be stored in a nested list of the form A = [[a, b, c], [d, e, f]]... and so on, based on a number of nested lists that the user desires. a, b, c, ... are integers; so I need to convert the user input...

pmax function on lists of lists

r,for-loop,nested-lists
I have some lists stored in lists. The third level contains a vector with numeric values and some NAs. I want to use pmax to find the highest value between two vectors that have the same position. pmax(low_all_res[[1]][[1]][[1]], high_all_res[[1]][[1]][[1]]) This works and outputs a list. But I want to do...

Mapping Odd/Even to nested lists

python,list,python-3.x,nested-lists
So I'm trying to manipulate a list with a loop. Changing my list into a string of each integer by changing them to even or odd. Input_List = [[1,2,3], [4,5,6], [7,8,9]] What I would like to have outputted is Input_List = [['odd','even','odd'],['even','odd','even'],['odd','even','odd']] This is the code I wrote so far:...

jQuery hide li based on the id from nested ul li

jquery,nested-lists
I need to hide the specific li (with id= 3-DELETED-) from nested ul li based on the id field. Example: The jquery I used: <script> $(document).ready(function(){ $("ul li").each(function () { var id = $(this).attr("id"); if (id.toLowerCase().indexOf('-deleted-') >= 0) { $(this).hide(); } }); }); </script> <ul> <li id="1">New</li> <li> <ul> <li...

“Faking” a sub-table of results from 3 tables, in SQL

mysql,sql,subquery,nested-lists
If I have a table of cases: CASE_NUMBER | CASE_ID | STATUS | SUBJECT | ---------------------------------------------------------------- 3108 | 123456 | Closed_Billable | Something Interesting 3109 | 325124 | Closed_Billable | Broken printer 3110 | 432432 | Open_Assigned | Email not working And a table of calls: PARENT_ID | STATUS |...

How do I do nested parenthesis in antlr4?

nested,grammar,dsl,antlr4,nested-lists
I have the following grammar query : unquoted_str+ | '(' query ')' ; unquoted_str : UNQUOTED_STR ; UNQUOTED_STR : [A-Za-z_][A-Za-z0-9_-]* ; WS : [ \t\r\n]+ -> skip ; I want to be able to execute the following query: abc (def (hij)) But I keep getting the error: line 1:4 extraneous...

Nested arithmetic operation on a list of lists

c#,nested-lists
I have a lstCmplx = List<List<CustomClass>>, the Custom Class looks like this: Public Class CustomClass { Decimal Value {get, set } Action Op {get, set } //This is an enum of operations (Add, Multiply...) } For each list in lstCmplx, I want to get the calculated result of all the...

create lag variable of xts object using $ vs. [] notation

r,xts,nested-lists
I am trying to create a lagged vector within an xts object using the lag function. It works when defining the new vector within the xts object using $ notation (e.g. x.ts$r1_lag), but it does when defining the new variable using square brackets, i.e. xts[,"r1_lag"]. See code below: library(xts) x...