Posts

Showing posts with the label list

My async call is returning before list is populated in forEach loop

My async call is returning before list is populated in forEach loop I have a routine which gets a list of filenames from the device, then reads the file(s) to build a list. However, the calling routine always returns with zero items. I print the filenames, so I know they exist, however, it appears that the async is returning before I read the files. I used similar code when making an HTTP call. But, something here is causing the routine to return the list even though it hasn't completed. Perhaps, it is possible that I am calling it at the wrong time? I am calling retrieveItems here: @override void initState() { super.initState(); retrieveItems(); } Eventually I will have a refresh button, but for now I'd simply like the list to populate with the data from the files... Callee Future<List<String>> readHeaderData() async { List<String> l = new List(); List<String> files = await readHeaders(); // Gets filenames files.forEach((filename...

Adding Multiple Elements of a list to another in C#

Adding Multiple Elements of a list to another in C# I have some lists like this: List A={1,2,3} List B={10,20,30} List C={100,200,300} ... and I want something like this: List ABC1={1,10,100} List ABC2={2,20,200} List ABC3={3,30,300} How can I do this in C#? can anyone help me? Are the lists stored in another list? – Tim Schmelter 23 secs ago 1 Answer 1 Use Union: A.Union(B).ToList(); A.Union(B).ToList(); By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Append list based on another element in list and remove lists that contained the items

Append list based on another element in list and remove lists that contained the items Let's say I have two lists like this: list_all = [[['some_item'],'Robert'] ,[['another_item'],'Robert'],[['itemx'],'Adam'],[['item2','item3'],'Maurice]] I want to combine the items together by their holder (i.e 'Robert') only when they are in separate lists. Ie in the end list_all should contain: list_all = [[['some_name','something_else'],'Robert'],[['itemx'],'Adam'],[['item2','item3'],'Maurice]] What is a fast and effective way of doing it? I've tried in different ways but I'm looking for something more elegant, more simplistic. Thank you Is keeping the results in this list format required for some reason? I would have thought a dict of lists with names as keys may make more sense – PerlPingu ...

R: How to concatenate vectors of strings in a list of vectors

R: How to concatenate vectors of strings in a list of vectors I have a list of vectors of strings like this: x=list(c("a","b"),"c",c("d","e","f"),c("gg","hh") ) I'd like to concatenate the vectors into single strings like this y=c("ab","c","def","gghh") I searched around but I couldn't find a similar example. Is there a smart way to do this without looping over the list elements? 2 Answers 2 With sapply : sapply y <- sapply(x, paste0, collapse = '') # [1] "ab" "c" "def" "gghh" You probably know this already, but FYI for those interested, paste0 and paste will give the same result in cases like this where there's only one argument (other than collapse ) – Ryan ...

Keeping track of which subset is computed to avoid mixing the results

Keeping track of which subset is computed to avoid mixing the results tricky name and unclear name... My issue is the following: import itertools t0 = 0 tf = 1000000 # inputs_to_compute = list-like of size 2 to 6 of objects results = [ for i in range(len(inputs_to_compute))] for subset in itertools.combinations(inputs_to_compute, 2): r1, r2 = compute(subset[0], subset[1], t0, tf) results[inputs_to_compute.index(subset[0])] += list(r1) results[inputs_to_compute.index(subset[1])] += list(r2) This code is creating as many results lists as there is an input. Each input is actually associated to a list. Then the computation is performed 2 by 2 (on each subset) and the result is added to the corresponding list. It works well as long as there is no repetition in the inputs because the method index returns the first occurrence of the element. How could I implement this differently (and efficiently, performance is one of the main issue I have) in a way that manage duplicates? index...

Unable to assign values to a pandas data frame column from a list suing iteration

Unable to assign values to a pandas data frame column from a list suing iteration I am changing my original code, to present a much simplified version of it. But, this is where the main problem is occurring. Using the following code: Sp=pd.DataFrame() l1=['a', 'b', 'c'] for i in l1: Sp['col1'] = i Gives me the result Sp as: col1 I would want my col1 to have values a, b and c. Could anyone please suggest why this is happening, and how to rectify it. EDIT: For every value in my list, I use it to connect to a different file using os, (file names are made up of list values). After picking up the csv file from there I take values such as mean, devisation etc. of the data from the file and assign those values to sp in another column. My final sp should look something as follows: col1 Mean Median Deviation a 1 1.1 0.5 b 2 2.1 0.5 c 3 3.1 0.5 @ Jezrael I am not using the loop ...

How can I (iteratively) save list elements to a data frame?

How can I (iteratively) save list elements to a data frame? I have a dataframe that contains several lists. The list I want to focus on specifically ( df$list ) contains product codes that I want to extract and save into a separate data frame. df$list To view the product code directly, I would write > df$list[[1]][8] product.sku_code 1 116932 As my data frame contains 528 rows, it is not plausible to do this manually. I have put together the following as a starting point, which prints out the value at each list location: for(i in 1:nrow(df)){ print(df$list[[i]][8]) } However, the problem here is that not all rows follow the same format; some contain lists, others don't, and the ones that do don't always contain a product code. As such, I get some results back and then I run into undefined columns selected errors and the loop exits undefined columns selected How can I perform the extraction of the codes if they are present and save them into a data frame? If it h...

Good way to iterate lists of different lengths and set default value

Good way to iterate lists of different lengths and set default value In Python, is there a good way to iterate through lists of different lengths? For example, a = [1,2,3] b=[4,5] c = [a,b] for val1, val2, val3 in c: print val1 print val2 print val3 Assuming that the list will have at least 2 values, and in some list, 3rd value is optional. The above for loop didn't work for b, obviously, that val3 is not available for list 'b'. In that case, I want to print the val3 as 0. Can I give a default value in case of unavailability? for for val1, val2, val3=0 in c: The above syntax didn't work either. Please help. 5 Answers 5 If you want to be fancy ("elegant"?), you can pad a given list with zeros: def pad_list(t, size, default): return t + [default] * (size - len(t)) for x in c: v1, v2, v3 = pad_list(x, 3, 0) print(v1) print(v2) print(v3) Similarly, if...

Making a 2 dimensional list/matrix with different number of columns for each row

Making a 2 dimensional list/matrix with different number of columns for each row I would like to make a list or matrix that has a known number of rows(3), but for each row the number of elements will be different. So it could look something like this: [[4, 6, 8], [1, 2, 3, 4], [0, 2, 3, 4, 8]] Each row will have a known maximum of elements(8). So far I have tried the following: Sets1=np.zeros((3,8)) for j in range(3): Sets1[0,:]=[i for i, x in enumerate(K[:-1]) if B[x,j]==1 or B[x+1,j]==1] I want this because I want to have a list for each j in range(3) over which I can do a for loop and add constraints to my ILP. Any help will be greatly appreciated! Final expected output for the given sample? – Divakar Jun 29 at 8:35 You shouldn't call that a matrix because matrix is a rectangular array of numbers. It's...

Efficient way to work with 2D Arrays (multiple formats)

Efficient way to work with 2D Arrays (multiple formats) I have an efficiency/performance question in Python: I am planning to store data in some kind of matrix format in a "storage" For example: elementID - nodeID - elementType - Value My current process is to loop through the raw data information and check if there is already an entry with "elementID, nodeID, elementType" in the storage. Now, when considering a second case, also a second value is occuring. What I want to do is now to add the second value to the associated case in the storage. So: elementID - nodeID - elementType - Value1 - Value2 Currently I am working with 2D arrays, so [[a,b,c],[b,c,d],....]. Since this is implying "for loops", the time required to check if the entry is already in the storage is increasing tremendously. Also to add the additional column to the storage is requiring the search for the corresponding entry. In some cases, it is also possible that there are many value column...

Python lists and for in Statements [on hold]

Python lists and for in Statements [on hold] def print_list(x): for a in x: print(x) c = [1, 2, 4, 5, 6] print(print_list(c)) Output: [1, 2, 4, 5, 6] [1, 2, 4, 5, 6] [1, 2, 4, 5, 6] [1, 2, 4, 5, 6] [1, 2, 4, 5, 6] In my textbook, it says that the code should simply generate the list however, in my console the list repeats 5 times and I used my debugger to see why it does that but it still does not illustrate how the output is 5 lines of the list. Help? This question appears to be off-topic. The users who voted to close gave this specific reason: typo, change print(x) to print(a) – eyllanesc 2 days ago print(x) print(a) 5 Answers 5 def print_list(x): for a in x: print(a) #print (a),not print(x) You must print(a), not x to print ea...