Posts

Showing posts with the label list

How to zip unequal lists as a product of first list with others?

How to zip unequal lists as a product of first list with others? I have multiple lists like following- list1=['Tom'] list2=[16] list3=['Maths','Science','English'] list4=['A','B','C'] I want to zip these lists to achieve the following mapping- desired results- [('Tom', 16, 'Maths','A'), ('Tom', 16, 'Science','B'), ('Tom', 16, 'English','C')] Result i am getting by using the following command- results=zip(list1,list2,list3,list4) [('Tom', 16, 'Maths','A')] this is just an example of my problem.If a generalised solution is provided it would be helpful. If I use the statement- res= itertools.izip_longest(*[x[c] for c in cols]) I am getting multiple rows but getting null for the name and age column. Also consider passing the column names in the above way since the names of columns are not static. Possible duplica...

Why do lists with the same data have different sizes?

Why do lists with the same data have different sizes? Say I create python lists in two ways. lists In the first case I use simple assignment: my_list = print(my_list, '->', my_list.__sizeof__()) my_list = [1] print(my_list, '->', my_list.__sizeof__()) my_list = [1, 1] print(my_list, '->', my_list.__sizeof__()) In the second case I use append() method on list: append() my_list = print(my_list, '->', my_list.__sizeof__()) my_list.append(1) print(my_list, '->', my_list.__sizeof__()) my_list.append(1) print(my_list, '->', my_list.__sizeof__()) But I get unexpected (for me) output: === WITH ASSIGNMENT === (, '->', 40) ([1], '->', 48) ([1, 1], '->', 56) === WITH APPEND === (, '->', 40) ([1], '->', 72) ([1, 1], '->', 72) What happens internally with Python memory management? Why do the 'same' lists have different sizes? I...

I need some help starting of this radio program

I need some help starting of this radio program What I need help on is getting the stations to change to the next station in the list if the user presses 3. class Radio: def __init__(self): self.stations=["STATIC","97.2", "99.6", "101.7", "105.3", "108.5"] self.stationStart=self.stations[0] def seekNext(self): self.stationsStart It starts at static but I want it to change every single one and then start over again. I tried something like this: stations=["STATIC","97.2", "99.6", "101.7", "105.3", "108.5"] a =input("enter 3 to seek next") while a !="0": if a =="3": print(stations[-1]) I only end up getting the last station cannot figure out how to list the rest of the stations. Using negative indices with lists operates on them in reverse order, so -1 is the last item, -2, second last and so ...

Raising elements of a list to powers indicated in another list

Raising elements of a list to powers indicated in another list I need to raise my list [23, 43, 32, 27, 11] to the powers indicated in this list [3, 5, 4, 3, 2]. Meanding the 23 should be raised to the power of 3, 43 to the power of 5 etc... I can do the whole list to one power with the help of this question: Raising elements of a list to a power but not like how I need. Should I use two loops? Many thanks for the help. [ x**y for x,y in zip([23,43,32,27,11],[3, 5, 4, 3, 2, 2]) ] – Amit Singh Jul 2 at 0:09 [ x**y for x,y in zip([23,43,32,27,11],[3, 5, 4, 3, 2, 2]) ] Yes there was a extra 2 in the 2nd list – Chloé Moreau Jul 2 at 0:34 2 Answ...

What is the quickest way in Python to identify lists with common elements A & B in indices [i] & [j] without using expensive for loops?

What is the quickest way in Python to identify lists with common elements A & B in indices [i] & [j] without using expensive for loops? Context: I went through many similar questions on the internet for list filters and tried all suggested solutions. But they all seem to be fine for comparing two different lists. But they do not give desired results for a my list-of-lists. Problem: I have a list of lists and two different numbers that I want to check if they are located in the first two index elements (index[0] and index[1]) of the nested lists within the list of lists. I wish to identify all such lists and then if such lists exist, compare the fourth index member of all those similar lists against a fixed number. My Sample list: [[1, 4, 65, 77, 22.0], [3, 2, 12, 55, 77.0], [1, 4, 16, 99, 13.0]] Numbers to check: `index[0] == 1 and index[1] == 4.` The above list of lists has two such nested lists where the first index member is 1 and second index member is 4. Hence we we now co...

Python pd using a variable with column name in groupby dot notation

Python pd using a variable with column name in groupby dot notation I am trying to use a list that holds the column names for my groupby notation. My end goal is to loop through multiple columns and run the calculation without having to re-write the same line multiple times. Is this possible? a_list = list(['','BTC_','ETH_']) a_variable = ('{}ClosePrice'.format(a_list[0])) proccessing_data['RSI'] = proccessing_data.groupby('Symbol').**a_variable**.transform(lambda x: talib.RSI(x, timeperiod=14)) this is the error I currently get because it thinks I want the column 'a_variable' which doesn't exist. AttributeError: 'DataFrameGroupBy' object has no attribute 'a_variable' use df[a_variable] instead of df.a_variable – RafaelC Jul 1 at 18:16 df...

Can I zip more than two lists together in Scala?

Can I zip more than two lists together in Scala? Given the following Scala List: val l = List(List("a1", "b1", "c1"), List("a2", "b2", "c2"), List("a3", "b3", "c3")) How can I get: List(("a1", "a2", "a3"), ("b1", "b2", "b3"), ("c1", "c2", "c3")) Since zip can only be used to combine two Lists, I think you would need to iterate/reduce the main List somehow. Not surprisingly, the following doesn't work: scala> l reduceLeft ((a, b) => a zip b) <console>:6: error: type mismatch; found : List[(String, String)] required: List[String] l reduceLeft ((a, b) => a zip b) Any suggestions one how to do this? I think I'm missing a very simple way to do it. Update: I'm looking for a solution that can take a List of N Lists with M elements each and create a List of M TupleNs. Update 2: ...

running a nested loop on a list of unknown length

running a nested loop on a list of unknown length I have defined a list as: comp = comp.append(["A", "B", "C", "D"]) comp.append(["E", "F", "I"]) In real case, I don't know the length of comp or comp[x] comp comp[x] Now, I am trying to run a nested loop on this, but failed. What I mean is, if I run my current snippet: for compsi in range(len(comp)): for elemn in range(len(comp[compsi])): print(comp[compsi][elemn]) the output is A B C D E F I . A B C D E F I What I am trying is, for each element of comp[0] , the full comp[1] will run, so that, I will get: A E F I B F I C F I D E F I and so; comp[0] comp[1] A E F I B F I C F I D E F I When I know I len(comp) = 2, I can easily do this using nested for loop as: len(comp) for lo in range(len(comp[0])): for l1 in range(len(comp[1])): print(...) But, how I can achieve the same when I don't know the lenght of comp ? comp Kindly help! Ok, say, we have ...

Accessing counts associated to each elements [duplicate]

Accessing counts associated to each elements [duplicate] This question already has an answer here: If a have a list of elements with theirs counts as below [('a', 1), ('b', 2), ('c', 2),('d', 3), ('e', 3)] And now I want to find out how many of them appear once,twice,and three times. So the expected output would be something like 1:1,2:2,3:2 The list is generated by the Counter function Counter(lst) Counter Counter(lst) This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. 2 Answers 2 Use another Counter on the values of your first Counter . Counter values Counter from collections import Counter s = 'abbccdddeee' c = Counter(s) counts = Counter(c.values()) Use collections.Counter again but this time count second element in each tuple. collections.Counter from ...

How to make empty lists in MATLAB Live Script and append to them both outside of a loop and inside one? [closed]

How to make empty lists in MATLAB Live Script and append to them both outside of a loop and inside one? [closed] The three list I want to make are the following and I'm not sure how it's done in MATLAB. na = nb = t Outside of the list I want to append the follwoing: NA = input('Enter intial number of atoms of A > '); NB = input('Enter intial number of atoms of B > '); tau_a = input('Define decay constant of A >'); tau_b = input('Define decay constant of B >'); time = input('the total time >'); dt = input('Define time increment >'); I've to append the values like so: na = [na, NA] nb = [nb; NB] t = [t, 0] Finally, inside the loop I have tried appending values like this: for i = int16(time/dt) nai = na[i]-(na[i]/tau_a)*dt nbi = nb[i]+((na[i]/tau_a)-(nb[i]/tau_b))*dt ti = t[i]+dt na = [na, nai] nb = [nb, nbi] t = [t, ti] end But it is not working. It does not want to append the values. ...

subset a dataframe based on a matrix of row numbers and save the result in one list

subset a dataframe based on a matrix of row numbers and save the result in one list I have a data frame called df that looks like: > df Date A B C 1 2001 1 12 14 2 2002 2 13 15 3 2003 3 14 16 4 2004 4 15 17 5 2005 5 16 18 6 2006 6 17 19 7 2007 7 18 20 8 2008 8 19 21 9 2009 9 20 22 10 2010 10 21 23 and a matrix called index that looks like: > index Resample01 Resample02 Resample03 Resample04 Resample05 [1,] 1 7 1 2 7 [2,] 3 9 2 3 8 [3,] 5 1 3 8 1 [4,] 8 3 4 9 4 [5,] 10 4 5 10 9 The numbers in each column stands for the row number to be selected. The aim is to split the dataframe into two exclusive groups of "train" and "test" according to the row numbers in each column of the matrix "index". For exampl...

How to use list.where function with the objects getting casted before getting an IEnumerable

How to use list.where function with the objects getting casted before getting an IEnumerable My classes are like this public interface ICar { CarModel GetCarModel(); } public class Honda: ICar { CarModel GetCarModel() { return CarModel.Honda; } } I have a list of ICars defined like this: ICars List<ICar> cars; I am looking to extract a IEnumerable of all cars of type Honda using the Where clause and casted to Honda and not to ICar. How do I do this? Is this possible? BTW more idiomatic C# would use a property not a method: public CarModel CarModel => CarModel.Honda; and it would need to be public to satisfy the interface. – Ian Mercer Jul 1 at 10:53 public CarModel CarModel => CarModel.Honda; public Not, that type design like this leads to "double-typing". Noting p...

Creating new list from 2 existing lists, where their values matches

Image
Creating new list from 2 existing lists, where their values matches I would like to do that in the screenshot. Could you please help me on that? Code: import numpy as np op = np.array([[46, 29], [39, 47], [25, 47], [31, 24]]) rt = np.array([[1, 1, 1, 0], # op[0][1]+op[1][1]+op[2][1]+op[3][0]= 29 + 47 + 47 + 31 = 154 [1, 0, 1, 1], [1, 0, 1, 0], [0, 1, 0, 1], [1, 1, 0, 0], [0, 1, 1, 1], [0, 1, 0, 1], [1, 1, 1, 0], [0, 1, 1, 0], [1, 0, 1, 0]]) Your code should be in the question so people can copy/paste it into their IDE/terminal. I'd suggest re-writing your question – AK47 9 mins ago 2 Answers 2 ...