Posts

Showing posts with the label list

remove matrix from a list of matrices

6 I have a list of 12 matrices called M and I'm trying to remove every matrix from the list that has 0 rows. I know that I can manually remove those matrices with (for example, to remove the second matrix) M[2] <- NULL . I would like to use logic to remove them with something like: M <- M[nrow(M)>0,] (but that obviously didn't work). r list matrix remove drop Share Improve this question Follow ...

Periodically replacing values in a list

Suppose I have the following list in Python: my_list = [10] * 95 Given n, I want to replace any other m elements with zero in my list, while keeping the next n elements. For example, if n = 3 and m = 2, I want my list to look like: [10, 10, 10, 0, 0, 10, 10, 10 ,0, 0, ..., 10, 10, 10 , 0, 0] If it can't be filled perfectly, as is the case with n = 4 and m = 2, then it's OK if my list looks like this: [10, 10, 10, 10, 0, 0, ..., 10, 10, 10, 10, 0] How should I try to solve this problem? my_list = [10] * 95 n = 3 m = 2 for i in range(m): my_list[n+i::m+n] = [0] * len(my_list[n+i::m+n]) This just needs m assignments to do the job (and m probably is small). If you really just have two possible values (e. g. 10 and 0), you can do it even simpler: my_list = [ 10 if i % (n+m) < n else 0 for i in range(95) ] But that iterates in Python over the whole range of 95, so probably is not very fast. A bit more complex but probably more efficient (especially for huge lists and larg...

How to sort a list by a private field?

My entity class looks like this: public class Student { private int grade; // other fields and methods } and I use it like that: List<Student> students = ...; How can I sort students by grade, taking into account that it is a private field? You have these options: make grade visible define a getter method for grade define a Comparator inside Student make Student implement Comparable use reflection (in my opinion this is not a solution, it is a workaround/hack) Example for solution 3: public class Student { private int grade; public static Comparator<Student> byGrade = Comparator.comparing(s -> s.grade); } and use it like this: List<Student> students = Arrays.asList(student2, student3, student1); students.sort(Student.byGrade); System.out.println(students); This is my favorite solution because: You can easily define several Comparators It is not much code Your field stays private and encapsulated Example of solution 4: public class Student implem...

Associating two item in a list

I am comparing two lists for common strings and my code currently works to output items in common from two lists. list1 ['5', 'orange', '20', 'apple', '50', 'blender'] list2 ['25', 'blender', '20', 'pear', '40', 'spatula'] Here is my code so far: for item1 in list1[1::2]: for item2 in list2[1::2]: if item1 == item2: print(item1) This code would return blender. What I want to do now is to also print the number before the blender in each list to obtain an output similar to: blender, 50, 25 I have tried to add two new lines to the for loop but did not have the desired output: for item1 in list1[1::2]: for item2 in list2[1::2]: for num1 in list1[0::2]: for num2 in list2[0::2]: if item1 == item2: print(item1, num1, num2) I know now that making for loops is not the answer. Also trying to call item1[-1] does not work....

Replace a word in list and append to same list

My List: city=['Venango Municiplaity', 'Waterford ship','New York'] Expected Result: city = ['Venango Municiplaity ', 'Waterford ship','New York','Venango','Waterford'] Common_words: common_words = ['ship','municipality'] Scan all the items in My List and strip the common words and re-insert in the same list as shown in Expected Result. I'm able to search the items which contains the common words but not sure how to replace that with blank and re-insert in My List. My code so far: for item in city: if(any(x in s.lower() for s in item.split(' ') for x in common_words)) : I have made a small code that works as expected: city=['Venango Municiplaity', 'Waterford ship','New York'] comwo = ['ship','municipality'] for i, c in enumerate(city): for ii in comwo: if ii in c: city.append(city[i].replace(ii,"")) print(city) Ou...

How to sum elements in list of dictionaries if two key values are the same

I have the following list of dictionaries: dictionary =[{'Flow': 100, 'Location': 'USA', 'Name': 'A1'}, {'Flow': 90, 'Location': 'Europe', 'Name': 'B1'}, {'Flow': 20, 'Location': 'USA', 'Name': 'A1'}, {'Flow': 70, 'Location': 'Europe', 'Name': 'B1'}] I want to create a new list of dictionaries, with summed Flow values of all dictionaries where Location and Name are the same. My desired output would be: new_dictionary =[{'Flow': 120, 'Location': 'USA', 'Name': 'A1'}, {'Flow': 160, 'Location': 'Europe', 'Name': 'B1'},] How can I achieve this? This is possible, but non-trivial to implement in python. Might I suggest using pandas? This is simple with a groupby, sum, and to_dict. import pandas as pd (pd...

Converting list of lists into a dictionary of dictionaries in Python

I am trying to convert a list of lists data structure to a dictionary of dictionaries. The list is defined as follows: l = [ ['PP','Ear-rings', 'Holesovice', 2000], ['PP','Skirts', 'Holesovice', 1000], ['PP','Dresses', 'E-shop', 1500], ['BM','Butterfly', 'Holesovice', 1600] ] My aim is to have the dictionary structure as follows: #{'PP' : {'Holesovice' : {'Ear-rings' : 2000, 'Skirts' : 1000}, # 'E-shop' : {'Dresses' : 1500}}, # 'BM' : {'Holesovice' : {'Butterfly' : 1600}} #} This bit of code does not return desired output: labels_d = {} items_d = {} shops_d = {} for index, row in enumerate(l): items_d[row[1]] = row[3] shops_d[row[2]] = items_d labels_d[row[0]] = shops_d print(labels_d) I found some posts that deal with converting lists to dictionaries here and here but I did not make it work the way...