I have dataframe column with strings, similar to:
'TCCTGTAAATCAAAGGCCAAGRG', 'GNGCNCCNGAYATRGCNTTYCC', 'GATTTCTCTYCCTGTTCTTGCA'
and I have a list of letter:
SNPs={}
SNPs["Y"] = ['C', 'T']
SNPs["R"] = ['A', 'G']
SNPs["N"] = ['C', 'G', 'A', 'T']
where every R needs to change to A/G and so on...
ex: TCCTGTAAATCAAAGGCCAAGRG
changes to TCCTGTAAATCAAAGGCCAAGAG and TCCTGTAAATCAAAGGCCAAGGG.
I want all permutation and combinations and the result in other column.
Please help me with the same.
import re, itertools
text = "GNGCNCCNGAYATRGCNTTYCC"
def getList(dict):
return list(dict.keys())
lsources = getList(SNPs)
ldests = []
for source in lsources:
ldests.append(SNPs[source])
#print(ldests)
# Generate the various pairings
for lproduct in itertools.product(*ldests):
#print(lproduct)
for i in text:
output = i
for src, dest in zip(lsources, lproduct):
# Replace each term (you could optimise this using a single re.sub)
output = output.replace("%s" % src, dest)
print(output)
this is my code..but I am not getting desired output
Try this:
>>> import itertools
>>> text = "GNGCNCCNGAYATRGCNTTYCC"
>>> SNPs={ "Y" : ['C', 'T'] , "R" : ['A', 'G'] , "N" : ['C', 'G', 'A', 'T']}
>>> text_tmp = ""
>>> dct = {}
>>> for idx, v in enumerate(text):
... if v in SNPs:
... dct[idx] = SNPs.get(v)
... text_tmp += f'_{idx}_'
... else:
... text_tmp += v
>>> text_tmp
'G_1_GC_4_CC_7_GA_10_AT_13_GC_16_TT_19_CC'
>>> dct
{1: ['C', 'G', 'A', 'T'],
4: ['C', 'G', 'A', 'T'],
7: ['C', 'G', 'A', 'T'],
10: ['C', 'T'],
13: ['A', 'G'],
16: ['C', 'G', 'A', 'T'],
19: ['C', 'T']}
>>> per_val = list(itertools.product(*dct.values()))
>>> per_key_val = list(map(dict,[zip(dct.keys(), p) for p in per_val]))
>>> per_key_val
[{1: 'C', 4: 'C', 7: 'C', 10: 'C', 13: 'A', 16: 'C', 19: 'C'},
{1: 'C', 4: 'C', 7: 'C', 10: 'C', 13: 'A', 16: 'C', 19: 'T'},
{1: 'C', 4: 'C', 7: 'C', 10: 'C', 13: 'A', 16: 'G', 19: 'C'},
{1: 'C', 4: 'C', 7: 'C', 10: 'C', 13: 'A', 16: 'G', 19: 'T'},
{1: 'C', 4: 'C', 7: 'C', 10: 'C', 13: 'A', 16: 'A', 19: 'C'},
{1: 'C', 4: 'C', 7: 'C', 10: 'C', 13: 'A', 16: 'A', 19: 'T'},
{1: 'C', 4: 'C', 7: 'C', 10: 'C', 13: 'A', 16: 'T', 19: 'C'},
{1: 'C', 4: 'C', 7: 'C', 10: 'C', 13: 'A', 16: 'T', 19: 'T'},
{1: 'C', 4: 'C', 7: 'C', 10: 'C', 13: 'G', 16: 'C', 19: 'C'},
{1: 'C', 4: 'C', 7: 'C', 10: 'C', 13: 'G', 16: 'C', 19: 'T'},
{1: 'C', 4: 'C', 7: 'C', 10: 'C', 13: 'G', 16: 'G', 19: 'C'},
{1: 'C', 4: 'C', 7: 'C', 10: 'C', 13: 'G', 16: 'G', 19: 'T'},
{1: 'C', 4: 'C', 7: 'C', 10: 'C', 13: 'G', 16: 'A', 19: 'C'},
{1: 'C', 4: 'C', 7: 'C', 10: 'C', 13: 'G', 16: 'A', 19: 'T'},
{1: 'C', 4: 'C', 7: 'C', 10: 'C', 13: 'G', 16: 'T', 19: 'C'},
{1: 'C', 4: 'C', 7: 'C', 10: 'C', 13: 'G', 16: 'T', 19: 'T'},
{1: 'C', 4: 'C', 7: 'C', 10: 'T', 13: 'A', 16: 'C', 19: 'C'},
{1: 'C', 4: 'C', 7: 'C', 10: 'T', 13: 'A', 16: 'C', 19: 'T'},
{1: 'C', 4: 'C', 7: 'C', 10: 'T', 13: 'A', 16: 'G', 19: 'C'},
{1: 'C', 4: 'C', 7: 'C', 10: 'T', 13: 'A', 16: 'G', 19: 'T'},
...
]
>>> out = []
>>> for pkl in per_key_val:
... tmp = text_tmp
... for k,v in pkl.items():
... tmp = tmp.replace(f'_{k}_', v)
... out.append(tmp)
>>> out
['GCGCCCCCGACATAGCCTTCCC',
'GCGCCCCCGACATAGCCTTTCC',
'GCGCCCCCGACATAGCGTTCCC',
'GCGCCCCCGACATAGCGTTTCC',
'GCGCCCCCGACATAGCATTCCC',
'GCGCCCCCGACATAGCATTTCC',
'GCGCCCCCGACATAGCTTTCCC',
'GCGCCCCCGACATAGCTTTTCC',
'GCGCCCCCGACATGGCCTTCCC',
'GCGCCCCCGACATGGCCTTTCC',
'GCGCCCCCGACATGGCGTTCCC',
'GCGCCCCCGACATGGCGTTTCC',
'GCGCCCCCGACATGGCATTCCC',
'GCGCCCCCGACATGGCATTTCC',
'GCGCCCCCGACATGGCTTTCCC',
'GCGCCCCCGACATGGCTTTTCC',
'GCGCCCCCGATATAGCCTTCCC',
'GCGCCCCCGATATAGCCTTTCC',
'GCGCCCCCGATATAGCGTTCCC',
'GCGCCCCCGATATAGCGTTTCC',
'GCGCCCCCGATATAGCATTCCC',
'GCGCCCCCGATATAGCATTTCC',
'GCGCCCCCGATATAGCTTTCCC',
...
]
Update: (run on dataframe)
def rplc_per(text):
SNPs={ "Y" : ['C', 'T'] , "R" : ['A', 'G'] , "N" : ['C', 'G', 'A', 'T']}
text_tmp = ""
dct = {}
for idx, v in enumerate(text):
if v in SNPs:
dct[idx] = SNPs.get(v)
text_tmp += f'_{idx}_'
else:
text_tmp += v
per_val = list(itertools.product(*dct.values()))
per_key_val = list(map(dict,[zip(dct.keys(), p) for p in per_val]))
out = []
for pkl in per_key_val:
tmp = text_tmp
for k,v in pkl.items():
tmp = tmp.replace(f'_{k}_', v)
out.append(tmp)
return out
df = pd.DataFrame({'String': ['TCCTGTAAATCAAAGGCCAAGRG', 'GNGCNCCNGAYATRGCNTTYCC', 'GATTTCTCTYCCTGTTCTTGCA']})
df['all_per'] = df['String'].apply(rplc_per)
print(df)
Output:
String all_per
0 TCCTGTAAATCAAAGGCCAAGRG [TCCTGTAAATCAAAGGCCAAGAG, TCCTGTAAATCAAAGGCCAA...
1 GNGCNCCNGAYATRGCNTTYCC [GCGCCCCCGACATAGCCTTCCC, GCGCCCCCGACATAGCCTTTC...
2 GATTTCTCTYCCTGTTCTTGCA [GATTTCTCTCCCTGTTCTTGCA, GATTTCTCTTCCTGTTCTTGCA]
Related
I'm trying to make a cipher based on Ceaser Cipher, which instead of letters, I used numbers. for example, 1 = 'a', 2 = 'b' and so on.
This is the full code:
import string
dict = {}
message = input("Enter a message\n")
key = input("Enter a key\n")
encrypted = ""
for i, char in enumerate(string.ascii_lowercase):
#key is from 0 to 25
dict[i] = char
print(dict)
for val in message:
if val in dict:
encrypted += dict[val]
for key, value in dict.items():
if val == value:
encrypted += str(key + 1)
encrypted += " "
print(encrypted)
in print(dict), it prints the value of the key
For example:
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h'}
What I want to do is to change the value based on the key input
For example:
key = 'D3'
My desired output:
{0: 'd', 1: 'e', 2: 'f', 3: 'a', 4: 'b', 5: 'c', 6: 'g', 7: 'h'}
Is there any solution for this?
This solution works only for the first time that the value_of_interest is found within the dict. In your case example - that s not the case, but I am not sure whether a more generalized approach of your cipher (I don t really know what exactly this is) ought to have this considered beforehand.
my_dict={0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h'}
print(f'Init dict: {my_dict}')
key_list=list(my_dict.keys())
#let's say ... (please make a more generalized parsing method for swaper)
swaper='D3'
print(f'Input swaper: {swaper}')
value_of_interest=swaper[0].lower()
shift_=int(swaper[1])
new_dict={}
for counter,key in enumerate(key_list):
if my_dict[key]==value_of_interest :
if len(key_list)>=counter+shift_: #Added a method for checking whether there are enough keys within the dict - after the D value is found.
new_dict = {i:my_dict[key] for i,key in enumerate(key_list[counter:counter+shift_])}
for i,key in enumerate(key_list[0:counter]):
new_dict[shift_+i]=my_dict[key]
for key in key_list[counter+shift_:]:
new_dict[key]=my_dict[key]
else:
print(f'There are not more than {shift_} keys following the {value_of_interest} value')
print(f'Output dict: {new_dict}')
Output:
Init dict: {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h'}
Input swaper: D3
Output dict: {0: 'd', 1: 'e', 2: 'f', 3: 'a', 4: 'b', 5: 'c', 6: 'g', 7: 'h'}
In another case for example, Given the value of "B5" in the swaper variable, it gives:
Init dict: {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h'}
Input swaper: B5
Output dict: {0: 'b', 1: 'c', 2: 'd', 3: 'e', 4: 'f', 5: 'a', 6: 'g', 7: 'h'}
I assume 'd3' means starting from d you are switching 3 values to the left side. If that's the case, 'd2' will return the following output:{0: 'd', 1: 'e', 2: 'c', 3: 'a', 4: 'b', 5: 'f', 6: 'g', 7: 'h'}
See if this code will work, I wrote a loop and it allows you to customize your key every time. d is the initial dictionary and k is the 'D3' key.
def swipe(d,k):
start = k[0].lower()
num = int(k[1])
count = 0
for i in d:
if d[i] == start:
break
else:
count+=1
for i in range(count,count+num):
d[i-count], d[i] = d[i],d[i-count]
return d
I have an example pandas dataframe, df, below:
{'column_a': {0: 'b', 1: 'b', 2: 'a', 3: 'b', 4: 'd', 5: 'a', 6: 'b', 7: 'b', 8: 'c', 9: 'a', 10: 'a', 11: 'a', 12: 'a', 13: 'c', 14: 'c', 15: 'c', 16: 'b', 17: 'a', 18: 'a', 19: 'b', 20: 'd', 21: 'c', 22: 'a', 23: 'b', 24: 'c', 25: 'c', 26: 'c', 27: 'e', 28: 'e', 29: 'e', 30: 'e', 31: 'c', 32: 'e', 33: 'e', 34: 'd', 35: 'e', 36: 'd', 37: 'e', 38: 'd', 39: 'b', 40: 'd', 41: 'c', 42: 'b', 43: 'd', 44: 'c', 45: 'e', 46: 'd', 47: 'c', 48: 'e', 49: 'b', 50: 'c'}, 'column_b': {0: 'c', 1: 'b', 2: 'b', 3: 'd', 4: 'b', 5: 'a', 6: 'd', 7: 'c', 8: 'c', 9: 'd', 10: 'a', 11: 'a', 12: 'b', 13: 'a', 14: 'c', 15: 'd', 16: 'd', 17: 'c', 18: 'b', 19: 'd', 20: 'a', 21: 'a', 22: 'd', 23: 'b', 24: 'a', 25: 'c', 26: 'e', 27: 'd', 28: 'b', 29: 'c', 30: 'd', 31: 'b', 32: 'e', 33: 'b', 34: 'b', 35: 'c', 36: 'b', 37: 'b', 38: 'd', 39: 'c', 40: 'b', 41: 'a', 42: 'b', 43: 'e', 44: 'e', 45: 'c', 46: 'e', 47: 'c', 48: 'b', 49: 'b', 50: 'c'}, 'column_c': {0: 'b', 1: 'd', 2: 'b', 3: 'b', 4: 'd', 5: 'c', 6: 'b', 7: 'a', 8: 'a', 9: 'a', 10: 'a', 11: 'b', 12: 'd', 13: 'c', 14: 'b', 15: 'a', 16: 'a', 17: 'a', 18: 'b', 19: 'c', 20: 'a', 21: 'a', 22: 'b', 23: 'd', 24: 'd', 25: 'c', 26: 'd', 27: 'c', 28: 'c', 29: 'e', 30: 'd', 31: 'c', 32: 'd', 33: 'c', 34: 'b', 35: 'b', 36: 'd', 37: 'd', 38: 'd', 39: 'b', 40: 'c', 41: 'e', 42: 'e', 43: 'b', 44: 'b', 45: 'd', 46: 'd', 47: 'c', 48: 'e', 49: 'd', 50: 'b'}, 'column_d': {0: 'b', 1: 'c', 2: 'd', 3: 'd', 4: 'b', 5: 'b', 6: 'd', 7: 'd', 8: 'd', 9: 'b', 10: 'd', 11: 'c', 12: 'b', 13: 'a', 14: 'c', 15: 'c', 16: 'd', 17: 'c', 18: 'd', 19: 'a', 20: 'd', 21: 'b', 22: 'd', 23: 'b', 24: 'd', 25: 'e', 26: 'c', 27: 'c', 28: 'c', 29: 'd', 30: 'c', 31: 'e', 32: 'd', 33: 'd', 34: 'd', 35: 'b', 36: 'c', 37: 'e', 38: 'b', 39: 'e', 40: 'b', 41: 'c', 42: 'b', 43: 'e', 44: 'b', 45: 'c', 46: 'd', 47: 'c', 48: 'c', 49: 'b', 50: 'd'}, 'target': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1, 10: 1, 11: 1, 12: 1, 13: 1, 14: 1, 15: 1, 16: 1, 17: 1, 18: 1, 19: 1, 20: 1, 21: 1, 22: 1, 23: 1, 24: 1, 25: 0, 26: 0, 27: 0, 28: 0, 29: 0, 30: 0, 31: 0, 32: 0, 33: 0, 34: 0, 35: 0, 36: 0, 37: 0, 38: 0, 39: 0, 40: 0, 41: 0, 42: 0, 43: 0, 44: 0, 45: 0, 46: 0, 47: 0, 48: 0, 49: 0, 50: 0}}
What I am trying to accomplish is to select a subsample of this dataframe of an arbitrary length or percentage, but in doing so, I want to maintain (as closely as possible) the frequency distributions of each value for each class.
For example, if I want to simply subsample the dataframe, I can use .sample() method
smaller_df = df.sample(n=100) or smaller_df = df.sample(frac=0.1)
However, it could be the case that the distributions of each value in each column in each class are lost. I need to preserve these value densities while downsampling my dataset size.
I can see these frequency densities with:
for col in df.columns:
print(df.groupby(['target'])[col].value_counts(normalize=True))
That output looks like:
target column_a
0 e 0.384615
c 0.269231
d 0.230769
b 0.115385
1 a 0.360000
b 0.320000
c 0.240000
d 0.080000
I have seen this post on Stack Overflow which seemingly answers that for a single distribution, but not multiple.
Ideally, how can I downsample my dataframe to maintain each columns frequency distribution with less samples? My actual dataset is (8370994, 731)
I am trying to write this dictionary comprehension and I don't know why all the dictionary values are the same.
The code is:
from string import ascii_uppercase
dictionary = {key: value for key in range(0, len(ascii_uppercase)) for value in ascii_uppercase}
The result is:
{0: 'Z', 1: 'Z', 2: 'Z', 3: 'Z', 4: 'Z', 5: 'Z', 6: 'Z', 7: 'Z', 8: 'Z', 9: 'Z', 10: 'Z', 11: 'Z', 12: 'Z', 13: 'Z', 14: 'Z', 15: 'Z', 16: 'Z', 17: 'Z', 18: 'Z', 19: 'Z', 20: 'Z', 21: 'Z', 22: 'Z', 23: 'Z', 24: 'Z', 25: 'Z'}
Why it is only giving me the last character of the string as all the values?
How can I fix it?
If you convert the dict comprehension into regular loops, you have this:
dictionary = {}
for key in range(0, len(ascii_uppercase)): # for every number in range 0-26
for value in ascii_uppercase: # for every letter in range a-z
dictionary[key] = value # d[num] = letter
The last letter for every number is 'z' so your dictionary is updated with it at the end of inner loop for each number.
You can try:
di = {}
for i, letter in enumerate(ascii_uppercase):
di[i] = letter
or
di = {i: letter for i, letter in enumerate(ascii_uppercase)}
or
di = dict(enumerate(ascii_uppercase))
Because the inner loop always end as the end of list, better is to Use enumerate() with dict() for this:
dictionary = dict(enumerate(ascii_uppercase))
The second for-loop gets fully executed before the first one so it messes up the result. You can try this approach with just one for-loop instead:
dictionary = {i: ascii_uppercase[i] for i in range(len(ascii_uppercase))}
Change your code to this
from string import ascii_uppercase
dictionary = {key: value for key,value in enumerate(ascii_uppercase, 0)}
Output
{0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F', 6: 'G', 7: 'H', 8: 'I', 9: 'J', 10: 'K', 11: 'L', 12: 'M', 13: 'N', 14: 'O', 15: 'P', 16: 'Q', 17: 'R', 18: 'S', 19: 'T', 20: 'U', 21: 'V', 22: 'W', 23: 'X', 24: 'Y', 25: 'Z'}
While there are other suitable answers, I believe this is what you think you are doing.
from string import ascii_uppercase
{key: value for key,value in zip(range(0, len(ascii_uppercase)),ascii_uppercase)}
I am trying to create dictionary with a reapeating pattern like
{0:"A",
1:"B",
2:"C",
3:"D",
4:"A",
5:"B",
6:"C",
7:"D",}
and so on. How would I do that? I have tried using for loops, but couldn't figure it out.
I'm not even sure this is the right approach to my problem. I am solving a simulation numerous times with the same output, only changing 1 input for every loop of the simulation.
Basically I end up with a DataFrame that collects the output (4 different series) for every simulation with columns
[0, 1, 2, 3, 4, 5, 6, 7, 8, ...]
which I would like to rename
["A", "B", "C", "D", "A", "B", "C", "D",...]
Alternatively, is there some sort of datatype in Python, which can provide 2 levels of categorizing like
[Simulation 1: ["A", "B", "C", "D"],
Simulation 2: ["A", "B", "C", "D"],
Simulation 3: ["A", "B", "C", "D"],
Simulation 4: ["A", "B", "C", "D"],
Simulation 5: ["A", "B", "C", "D"],
and so on...]
where "A", "B", "C" and "D" each contains a column of data output, that is different for every simulation?
You can achieve this neatly with itertools.cycle:
In [1]: import itertools
In [2]: cols = [0, 1, 2, 3, 4, 5, 6, 7]
In [3]: dict(zip(cols, itertools.cycle('ABCD')))
Out[3]: {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'A', 5: 'B', 6: 'C', 7: 'D'}
If you'd rather not import modules you could use dictionary comprehension with a modulus operator (%)
print({i:'ABCD'[i%4] for i in range(12)})
{0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'A', 5: 'B', 6: 'C', 7: 'D', 8: 'A', 9: 'B', 10: 'C', 11: 'D'}
If you want to use a for-loop, you could use the modulo operator along with string.ascii_uppercase:
>>> from string import ascii_uppercase
>>> n = 8
>>> repeat_every = 4
>>> d = {i: ascii_uppercase[i % repeat_every] for i in range(n)}
>>> d
{0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'A', 5: 'B', 6: 'C', 7: 'D'}
Alternatively, is there some sort of datatype in Python, which can
provide 2 levels of categorizing like...
You could use itertoools.permutations inside a dict comprehension:
>>> from itertools import permutations
>>> from string import ascii_uppercase
>>>
>>> def pretty_print_simple_dict(d):
... print("{")
... for k, v in d.items():
... print(f"\t{k}: {v}")
... print("}")
...
>>> repeat_every = 4
>>> d = {
... f"Simulation {i + 1}": list(p)
... for i, p in enumerate(permutations(ascii_uppercase[:repeat_every]))
... }
>>>
>>> pretty_print_simple_dict(d)
{
Simulation 1: ['A', 'B', 'C', 'D']
Simulation 2: ['A', 'B', 'D', 'C']
Simulation 3: ['A', 'C', 'B', 'D']
Simulation 4: ['A', 'C', 'D', 'B']
Simulation 5: ['A', 'D', 'B', 'C']
Simulation 6: ['A', 'D', 'C', 'B']
Simulation 7: ['B', 'A', 'C', 'D']
Simulation 8: ['B', 'A', 'D', 'C']
Simulation 9: ['B', 'C', 'A', 'D']
Simulation 10: ['B', 'C', 'D', 'A']
Simulation 11: ['B', 'D', 'A', 'C']
Simulation 12: ['B', 'D', 'C', 'A']
Simulation 13: ['C', 'A', 'B', 'D']
Simulation 14: ['C', 'A', 'D', 'B']
Simulation 15: ['C', 'B', 'A', 'D']
Simulation 16: ['C', 'B', 'D', 'A']
Simulation 17: ['C', 'D', 'A', 'B']
Simulation 18: ['C', 'D', 'B', 'A']
Simulation 19: ['D', 'A', 'B', 'C']
Simulation 20: ['D', 'A', 'C', 'B']
Simulation 21: ['D', 'B', 'A', 'C']
Simulation 22: ['D', 'B', 'C', 'A']
Simulation 23: ['D', 'C', 'A', 'B']
Simulation 24: ['D', 'C', 'B', 'A']
}
I am working on ways to get me thinking in python. I have a simple idea that will take a number and give the corresponding "value" from a dictionary.
So basically I would like to have a number or numbers given, and then turn those numbers into a word.
The overall all view is to have a dictionary with keys ranging for 1 to 26 with values going from a to z. So 1 would equal "a" and 26 would equal "z".
I have a variable d = 1, and need to get the output of 'a'. Then increase size of this function for a list like (1,2,3,4) which output would be a, b, c, d.
Here is what I have so far.
d = 1
def code_scram(x):
c = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}
scram = ""
for i in d:
if i in c:
scram += c[i]
return scram
print code_scram(d)
However, its not working out as planned.
Your for loop should iterate through x, not d.
def code_scram(x):
c = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}
scram = ""
for i in x:
if i in c:
scram += c[i]
return scram
print code_scram([1,2,3,4])
Result:
abcd
The function only works for lists, so passing in the integer d won't work. Pass in a list instead.
d = [1]
print code_scram(d)
If you want the function to work for lists and lone integers, you can perform a type check, and convert as necessary.
def code_scram(x):
if isinstance(x, int):
x = [x]
c = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}
scram = ""
for i in x:
if i in c:
scram += c[i]
return scram
d = 1
print code_scram(d)
Result:
a