I have a dictionary as follows:
s_dict = {'s' : 'ATGCGTGACGTGA'}
I want to change the string stored as the value of the dictionary for key 's' at positions 4, 6, 7 and 10 to h, k, p and r.
pos_change = {'s' : ['4_h', '6_k', '7_p', '10_r']}
The way I can think about it is in a loop:
for key in s_dict:
for position in pos_change[key]:
pos = int(position.split("_")[0])
char = position.split("_")[1]
l = list(s_dict[key])
l[pos]= char
s_dict[key] = "".join(l)
Output:
s_dict = {'s': 'ATGChTkpCGrGA'}
This works fine but my actual s_dict file is about 1.5 Gb. Is there a faster way of replacing a list of characters at specific indices in a string or list?
Thanks!
pos_changewould be better as a dict of dicts (pos_change = {'s' : {4: 'h', 6: 'k', 7: 'p', 10: 'r'}})s_dict['s'] = '%s%s%s' % (s_dict['s'][:pos], char, s_dict['s'][pos+1:])instead of do list and joinbytearrayas it is mutable