create possible numbers from list in python - python-3.x
I want to create possible list of numbers from given list. For ex.
a = [5, 9]
should result in 59, 95.
How do I do it in python. Please note contents of list a could be variable.
Thanks in advance
This might be helpful.
import itertools
a = [5,9]
a = "".join(str(i) for i in a)
x = list(itertools.permutations(a))
x = ["".join(tupl) for tupl in x]
print(x)
['59', '95']
Related
Find the most frequent number in a 3d list
I can't find a way to show the most frequent number in this list a = [1,2,4,5,6,7,15,16,19,23,24,26,27,28,29,30,31,33,36,37,38,39,40,41,42,43,44,45,47,48,49,50,51,52,56,57,58,60] b = [1,3,4,5,6,8,9,10,15,16,17,18,20,21,22,24,26,28,29,31,32,33,36,37,38,40,41,43,44,47,48,50,52,53,54,56,57,58,60] c = [2,3,5,6,8,9,12,13,17,19,20,23,25,26,27,28,29,30,31,33,34,35,36,37,40,44,45,47,48,52,53,54,55,56,57,58,60] d = [2,5,7,9,11,12,13,14,16,18,20,22,23,26,29,30,33,34,36,38,40,41,42,43,44,46,47,49,50,51,53,56,57,58,60] list_1 = [a,b] list_2 = [c,d] lists = [list_1, list_2] I have tried the collections library with the most_common() funtion but it does't seem to work. Same happens with numpy arrays. It would be perfect if I could get the top 10 most common number too. The reason for the list to be multi-dimensional is for easy comparison between months Jan_22 = [jan_01, jan_02, jan_03, jan_04] Fev_22 = [fev_01, fev_02, fev_03, fev_04] months = [Fev_22, Jan_22] Each month has 4 data sets, making those lists allows me to compare big chunks of data, Top 10 most common values from 2021, most common number in jan, fev, mar, April, may ,jun. Would make it easier and clear Thanks
Maybe I don't fully understand your question, but I don't understand why the list needs to be multi-dimensional if you only want to know the frequency of a given value. import pandas as pd a = [1,2,4,5,6,7,15,16,19,23,24,26,27,28,29,30,31,33,36,37,38,39,40,41,42,43,44,45,47,48,49,50,51,52,56,57,58,60] b = [1,3,4,5,6,8,9,10,15,16,17,18,20,21,22,24,26,28,29,31,32,33,36,37,38,40,41,43,44,47,48,50,52,53,54,56,57,58,60] c = [2,3,5,6,8,9,12,13,17,19,20,23,25,26,27,28,29,30,31,33,34,35,36,37,40,44,45,47,48,52,53,54,55,56,57,58,60] d = [2,5,7,9,11,12,13,14,16,18,20,22,23,26,29,30,33,34,36,38,40,41,42,43,44,46,47,49,50,51,53,56,57,58,60] values = pd.Series(a + b + c + d) print(values.value_counts().head(10)) print(values.value_counts().head(10).index.to_list())
I dont get why are you adding up lists in each step to get a 3D element, you could just use arrays or smth like that, but here is a function that does what you want in a 3d list (returns the x most common elements in your 3d list ,a.k.a list of lists): import numpy as np arr = [[1,2,4,5,6,7,15,16,19,23,24,26,27,28,29,30,31,33,36,37,38,39,40,41,42,43,44,45,47,48,49,50,51,52,56,57,58,60], [1,3,4,5,6,8,9,10,15,16,17,18,20,21,22,24,26,28,29,31,32,33,36,37,38,40,41,43,44,47,48,50,52,53,54,56,57,58,60], [2,3,5,6,8,9,12,13,17,19,20,23,25,26,27,28,29,30,31,33,34,35,36,37,40,44,45,47,48,52,53,54,55,56,57,58,60], [2,5,7,9,11,12,13,14,16,18,20,22,23,26,29,30,33,34,36,38,40,41,42,43,44,46,47,49,50,51,53,56,57,58,60]] def x_most_common(arr, x): l = [el for l in arr for el in l] output = list(set([(el, l.count(el)) for el in l])) output.sort(key= lambda i: i[1], reverse=True) return output[:x] # test: print(x_most_common(arr, 5)) output: [(56, 4), (36, 4), (47, 4), (58, 4), (5, 4)]
Using Recursion to get lists of original list in Python returns error
I am given a task to use recursion so that any list that is given to it say p would be return in the desired output as shown at the end without disturbing or altering the contents of p. When I run this function it says that local variable (y) is referenced without assignment i.e. len(y)==0 but I have already assigned y outside the function init. Can someone help identify what's wrong? y=[] #To copy contents of x in it so that x is not modified z=[] # To return it as a list required in the question p=[1,2,3] def inits(x): if len(y)==0 and len(z)==0: y=[i.copy for i in x] #Copying contents of x into x so that it remains unchanged if len(z)==len(x)+1: #z list is one plus x list because it has [] in it print(z) else: z.append(y.copy()) if len(y)!=0: #This is done so that when y=[] it does not return error for y.pop(-1) y.pop(-1) inits(x) inits(p) #Desired Output [[1,2,3],[1,2],[1],[]]
If you don't need the last empty element then feel free to remove the else block. Your y variable is also not needed as for me z = [] # To return it as a list required in the question def foo(param): if len(param): z.append([i for i in param]) foo(param[0:-1]) else: z.append([]) foo([1, 2, 3]) print(z) [[1, 2, 3], [1, 2], [1], []]
How can i assign a sequence of random string values to a variable? (Python)
import random for x in range (10): print (random.choice('GACT'), end=random.choice('GACT')) So I want the output of this code into a variable.
>>> x = ''.join([random.choice('GACT') for i in range(10)]) >>> x 'CGGTGCCCTG' Maybe something like this is what you are looking for?
How to combine the whole line in an excel into one list element?
Excel I am a student and currently studying python. I want to make a program so I can combine the whole line in an excel into one list element. How am I suppose to do that? Currently, I can get the data from the excel into a string. How do I combine them and turn them into a list with each line as an element? How do I write this excel into a dictionary? Thank in advance. Below is my code: import csv def getMedalStats(): fLocation="C://TEMP//" print("Assumed file location is at: ", fLocation) fName = input("\nPlease enter a file name with its extension (ex. XXX.txt): ") fin = open(fLocation + fName, 'r') aStr = fin.read() return aStr #********************************************************************************* def main(): eventList = getMedalStats() print(eventList)
I strongly recommend using pandas.read_excel() to do this. Pandas is a very helpful module that makes data manipulation super easy and convenient. As a starting point, you can do the followings: import pandas as pd fin = pd.read_excel(fLocation + fName) This would return fin as a pandas dataframe, which you can then play with. As an example of how to manipulate dataframes: d = {'col1': [1, 2, 3], 'col2': [3, 4, 5], 'col3': ['3a', '4b', '5d']} df = pd.DataFrame(data=d) If you want to turn the first row into a list, you can do: list(df.loc[0]) where 0 is the index of the first row. Or if you want to turn the first column into a list, you can do: list(df.loc[:,'col1']) where col1 is the name of the first column. Or if you want to turn the whole excel into a list with each row as an element (by concatenating all values in each row): df['all']=df.astype(str).apply(''.join, axis=1) list(df.loc[:,'all']) Let me know if it doesn't work or you need any other help!
Python 3.x random input for lists
from random import randint List = [randint(0,99)*20] print(List) How could i go along the lines of make it into a list for 20 different random numbers between 0 and 99? The code i have ends up multiplying one random number 20 times.
You can use list comprehension: List = [randint(0,99) for i in range(20)]
from random import randint List = [randint(0,99) for i in range(20)] print("%3s" % List) List.sort() print("%3s" % List) My primary is Java. I hope this helps!