Formatting Output of Itertools.Products - python

I'm new to python and coding (last night). I need to generate a very large number of itertools products with a specific format for the output. I can generate the combinations using,
import itertools
s=[ ['CPT1','OTHERCPT1','OTHERCPT2','OTHERCPT3','OTHERCPT4','OTHERCPT5','OTHERCPT6','OTHERCPT7','OTHERCPT8','OTHERCPT9','OTHERCPT10','CONCURR1','CONCURR2','CONCURR3','CONCURR4','CONCURR5','CONCURR6','CONCURR7','CONCURR8','CONCURR9','CONCURR10'], ['15756','15757','15758','43496','49006','20969','20955','20956','20957','20962','20970','20972','20973'],['CPT1','OTHERCPT1','OTHERCPT2','OTHERCPT3','OTHERCPT4','OTHERCPT5','OTHERCPT6','OTHERCPT7','OTHERCPT8','OTHERCPT9','OTHERCPT10','CONCURR1','CONCURR2','CONCURR3','CONCURR4','CONCURR5','CONCURR6','CONCURR7','CONCURR8','CONCURR9','CONCURR10'], ['15756','15757','15758','43496','49006','20969','20955','20956','20957','20962','20970','20972','20973']]
x=list(itertools.product(*s))
print x
however the output appears as such:
('CPT1', '15756', 'CPT1', '15756'), ... etc.
I would like it to appear:
SELECT IF(CPT1='15756' AND CPT1='15756').
SELECT IF(...).
etc.
Thanks for your help!

You should use string formatting (https://docs.python.org/2/library/string.html)
import itertools
s = [[...first list...],[...second list...]]
for p in itertools.product(*s):
print("SELECT IF(CPT1='{}' AND CPT1='{}').".format(*p))

Related

In Python, what is the best way/data structure to rollup account codes or any other type of data that is linked to one another?

52.13.03.014.10
Above I have an account code that needs to be rolled up as such:
52
52.13
52.13.03
52.13.03.014
52.13.03.014.10
Other than string manipulation, how can I do this roll up while starting with the account code: 52.13.03.014.10 ?
I start with the full account code and would like to insert the roll up into excel like this:
Excel Picture
Using itertools.accumulate:
s = '52.13.03.014.10'
from itertools import accumulate
out = list(accumulate(s.split('.'), lambda *x: '.'.join(x)))
Output:
['52', '52.13', '52.13.03', '52.13.03.014', '52.13.03.014.10']
NB. You can also use lambda a,b: a+'.'+b as function in accumulate.

i want to make random string from given information in python

like I got some idea to create Instagram user finder from name and last name
i know it will be not that accurate but i just want to create so
import random
first_name="abc"
last_name="xyz"
first_name_list=list(first_name)
last_name_list=list(last_name)
some_extra=[".","_","-","#"]
numbers = []
for i in range(10001):
numbers.append(i)
genuser=[]
genuser_len = len(genuser)
for i in range(genuser_len):
user_name=genuser[l]
I want to create a bunch of username with this information
now I have no idea how to generate list
please help me
i want to generate usernames like this a_xyz123, abc_xyz, abc.xyz, a.xyz023
You can try this.
import random
first_name="abc"
last_name="xyz"
first_name_list=list(first_name)
first_name_list.append(first_name)
last_name_list=list(last_name)
last_name_list.append(last_name)
some_extra=[".","_","-","#"]
numbers = [i for i in range(10001)]
genuser=[]
genuser_len = len(genuser)
for i in numbers:
list1=[]
x=random.choice(first_name_list) #== Random choice from first_name_list
y=random.choice(last_name_list) #== Random choice from last_name_list
z=random.choice(some_extra) #== Random choice from some_extra
x1=random.randint(1,10001)
list1.append(x)
list1.append(y)
list1.append(str(x1))
random.shuffle(list1) #=== Shuffle list
k=random.randint(1,2)
list1.insert(k,z)
print(''.join(list1))
Sample output:
abc120-xyz
4132#xa
c5336-y
8355_cy
abcz#3168
a-z9931
a.783x
9323x.c
x1980.c
z_3948a
This is a permutations problem, and the output result is going to be enormously large for the given requirement, you can try something like this:
from itertools import permutations
list(map(lambda x: ''.join(map(str,x)),permutations(first_name_list+last_name_list+some_extra+numbers, 3)))
PS: To avoid lage computational time and high memory usage for this example, I'm using range(11) for the numbers list, and I'm using 3 as the width of user names generated which is passed to permutations function above.
SMALL SLICE OF OUTPUT:
['ab2', 'ab3', 'ab4', 'ab5', 'ab6', 'ab7', 'ab8', 'ab9', 'ab10', 'acb', 'acx', 'acy', 'acz', 'ac.', 'ac_', 'ac-', 'ac#', 'ac0', 'ac1', 'ac2']

How to print selected numbers from a list in python?

I want to print all atoms except H (hydrogen) from the pdb file. Here is the file
https://github.com/mahesh27dx/molecular_phys.git
Following code prints the objects of the file
import numpy as np
import mdtraj as md
coord = md.load('alanine-dipeptide-nowater.pdb')
atoms, bonds = coord.topology.to_dataframe()
atoms
The result looks like this
From this table I want to print all the elements except H . I think it can be done in python using the list slicing. Could someone have any idea how it can be done?
Thanks!
You probably should clarify that you want help with mdtraj or pandas specifically.
Anyway, it's as simple as atoms.loc[atoms.element != 'H'].
You can do the following:
print(*list(df[df.element!='H']['element']))
If you want the unique elements, you can do this:
print(*set(df[df.element!='H']['element']))

How do I put a set of random letters into an aligned string in python 3?

I'm trying to create a random set of letters and put it inside a string, and I'm using python 3.6 here (I have tested some other ways to do this, but they are outdated and only work in the earliest versions of python 3 and below).
I am using this:
import random
import string
randomletters = random.sample(string.ascii_letters, 4)
for randomletters in randomletters: # gather the elements into a string
stringofrndletters = "".join(randomletters)
print(stringofrndletters)
The output looks like:
>> print(stringofrndletters)
r
B
X
g
But I want the output to be similar to this:
>> print(stringofrndletters)
rBXg
No need for a for loop.
randomletters = random.sample(string.ascii_letters, 4)
print(''.join(randomletters))

My code doesn't output the full parametric equation only the first value in python

I am trying to find the parametric equations for a certain set of plots, however when I implement the code I only get the first value back. The code is from a website, as I am not very proficient in python. I am using 3.6.5. Here is the code:
import numpy as np
import scipy as sp
from fractions import Fraction
def trigSeries(x):
f=sp.fft(x)
n=len(x)
A0=abs(f[0])/n
A0=Fraction(A0).limit_denominator(1000)
hn=np.ceil(n/2)
f=f[1:int(hn)]
A=2*abs(f)/n
P=sp.pi/2-sp.angle(f)
A=map(Fraction,A)
A=map(lambda a:a.limit_denominator(1000),A)
P=map(Fraction,P)
P=map(lambda a:a.limit_denominator(1000),P)
s=map(str,A)
s=map(lambda a: a+"*np.sin(", s)
s=map(lambda a,b,c :
a+str(b)+"-2*sp.pi*t*"+str(c)+")",
s,P,range(1,len(list(P))+1))
s="+".join(s)
s=str(A0)+"+"+s
return s
x=[5041,4333,3625,3018,2816,2967,3625,4535,5800,6811,7823,8834,8429,7418,6305,5193,4181,3018,3018,3777,4687,5496,6912,7974,9087]
y=[4494,5577,6930,8825,10990,13426,14509,15456,15456,15186,15321,17486,19246,21005,21276,21952,22223,23712,25877,27501,28178,28448,27636,26960,25742]
xf=trigSeries(x)
print(xf)
Any help would be appreciated.
I tried to make the code to work but i could not manage to do it.
The problem id that when you call map(...) You create an iterator, so in order to print it's content you have to do:
for data in iterator:
print(data)
The problem her is that when you apply the lambda function if you cycle over the variable it returns nothing.
You colud convert all the lambda in for cycles, but you have to think over the triple-argument lambda.
The problem is at the step
s=map(lambda a,b,c :
a+str(b)+"-2*sp.pi*t*"+str(c)+")",
s,P,range(1,len(list(P))+1))
it returns empty list. To resolve it, convert s and P to lists just before feeding to this map function. Add two lines above.
s = list(s)
P = list(P)
Output for your example
134723/25+308794/391*np.sin(-1016/709-2*sp.pi*t*1)+2537094/989*np.sin(641/835-2*sp.pi*t*2)+264721/598*np.sin(-68/241-2*sp.pi*t*3)+285344/787*np.sin(-84/997-2*sp.pi*t*4)+118145/543*np.sin(-190/737-2*sp.pi*t*5)+281400/761*np.sin(-469/956-2*sp.pi*t*6)+1451/8*np.sin(-563/489-2*sp.pi*t*7)+122323/624*np.sin(-311/343-2*sp.pi*t*8)+115874/719*np.sin(-137/183-2*sp.pi*t*9)+171452/861*np.sin(-67/52-2*sp.pi*t*10)+18152/105*np.sin(-777/716-2*sp.pi*t*11)+24049/125*np.sin(-107/76-2*sp.pi*t*12)

Categories