I have the following fasta file in a dictionary, in the following shape:
from Bio import SeqIO
alignment_file = '/Users/dissertation/Desktop/Alignment 4 sequences.fasta'
seq_dict = {rec.id : rec.seq for rec in SeqIO.parse(alignment_file, "fasta")}
Which gives me the following input:
{'NC_000962.3': Seq('ctgttaccgagatttcttcgtcgtttgttcttggaaagacagcgctggggatcg...NNN'),
'NC_008596.1': Seq('------------------------------------------------------...ccg'),
'NC_009525.1': Seq('ctgttaccgagatttcttcgtcgtttgttcttggaaagacagcgctggggatcg...NNN'),
'NC_002945.4': Seq('ctgttaccgagatttcttcgtcgtttgttcttggaaagacagcgctggggatcg...NNN')}
The only issue here is that I would like to replace the key names for other than easier to identify when comparing the sequences to other parts of my code. So I have tried the following:
name_list = ['Tuberculosis', 'Smegmatis', 'H37Ra', 'Bovis']
for key in seq_dict:
for name in name_list:
seq_dict[name[x]]= seq_dict[key]
seq_dict
However I get the following error:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
/var/folders/pq/ghtv3wj159j681vy0ny3tz9w0000gp/T/ipykernel_47822/1486954832.py in <module>
9
---> 10 for key in seq_dict:
11 for name in name_list:
12 seq_dict[name[x]]= seq_dict[key]
RuntimeError: dictionary changed size during iteration
I understand that there's not an easy straight forward way of updating key names values in a dictionary, but I don't understand the error. Is there a way of doing something similar?
I have also tried this:
seq_dict.update({'NC_000962.3': 'Tuberculosis', 'NC_008596.1': 'Smegmatis', 'NC_009525.1': 'H37Ra', 'NC_002945.4': 'Bovis'})
But this gives me the following output:
{'NC_000962.3': 'Tuberculosis',
'NC_008596.1': 'Smegmatis',
'NC_009525.1': 'H37Ra',
'NC_002945.4': 'Bovis'}
My desire output would look like this:
{'Tuberculosis': Seq('ctgttaccgagatttcttcgtcgtttgttcttggaaagacagcgctggggatcg...NNN'),
'Smegmatis': Seq('------------------------------------------------------...ccg'),
'H37Ra': Seq('ctgttaccgagatttcttcgtcgtttgttcttggaaagacagcgctggggatcg...NNN'),
'Bovis': Seq('ctgttaccgagatttcttcgtcgtttgttcttggaaagacagcgctggggatcg...NNN')}
Does anybody have an idea on how to update these?
Construct a new dictionary and then assign it to seq_dict in a single operation, rather than mutating seq_dict as you're in the process of iterating over it. I think this is what you're aiming for:
seq_dict = dict(zip(name_list, seq_dict.values()))
although I'd personally want to have an explicit mapping from sequence IDs to names rather than relying on the ordering being the same.
So I am creating 10 dictionaries from a data-frame.
I have already done 3 for each row, but I have decided to do one for every column in my data-frame. When I add the 7 additional dictionaries, I get a float object not subscriptable error. What's confusing is, I had already added the additional 7 dictionary entries for a few other rows. Even more confusing, the error is on a line where the dictionary entries had already been successfully assigned and not for the entries I'm adding to one of the 7 additional dictionaries. Here's my code, please help if you can.
pace[b[1]] = bList[1]
offEff[b[1]] = bList[9]
defEff[b[1]] = bList[10]
ast[b[1]] = bList[2]
to[b[1]] = bList[3]
orr[b[1]] = bList[4]
drr[b[1]] = bList[5]
rebr[b[1]] = bList[6]
effFG[b[1]] = bList[7]
tsPer[b[1]] = bList[8]
I'm using JupyterLab, if that helps.
You should check if the bList is a list object.According to your description,the bList may be a float in your code:
>>> a=1.0
>>> a[1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'float' object is not subscriptable
How to get the values from a list of dictionary??
For example: I want to get the output as 2?
data = [{'a':1,'b':2,'c':3}]
Below is the error am observing.
data['b']
Traceback (most recent call last):
File "", line 1, in
data['b']
TypeError: list indices must be integers, not str
The dictionary is the first element in data, so you can access it with:
data[0]['b']
If you have multiple dictionaries and you aim want to use them in a fallback order, you can use:
def obtain_value(list_of_dicts,key):
for dict in list_of_dicts:
if key in dict:
return dict[key]
raise KeyError('Could not find key \'%s\''%key)
and then call it with obtain_value(data,'b').
for line in open('transactions.dat','r'):
item=line.rstrip('\n').split(',')
custid=item[2]
amt=item[4]
if custid in cust:
amt1=int(cust[custid])+int(amt)
cust[custid]=amt1
else:
cust[custid]=[amt]
well i am trying to check if customer id already is there in dictionary then simply add the previous amount and new amount in that customer. Otherwise add that amount in a new position in list. But i am getting error:
Traceback (most recent call last):
File "<pyshell#74>", line 7, in <module>
amt1=int(cust[custid])+int(amt)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'
some transaction data is like:
101300101,2016-09-03,376248582,1013,10.92
109400132,2016-09-03,391031719,1094,36.72
136100107,2016-09-03,391031719,1361,28.77
Did you try using defaultdict? It would make your job much easier.
from collections import defaultdict
cust = defaultdict(int)
for line in open('transactions.dat','r'):
item=line.rstrip('\n').split(',')
custid=item[2]
amt=item[4]
cust[custid] += float(amt)
and why do you try to cast amt to int? Looks like it's not an integer in the sample lines you posted. If you really want to use integer change float(amt) to int(float(amt)).
I have an issue with a code I've written. When I run the code I get an error :
Traceback (most recent call last):
File "test23_json_users.py", line 23, in <module>
for user_dict in abc['user_account']['sip_id']:
TypeError: list indices must be integers, not str
The code:
result = '[{"user_account":[{"address":null,"name":null,"country":null,"password":"****","extension":"1112","sip_id":"23001#50.50.50.201","sip_name":"23001","user_id":7973712,"locked":false,"created":null,"h323_name":"1101","city":null,"email":null,"phone":null,"zip":null},{"address":null,"name":null,"country":null,"password":"****","extension":"1113","sip_id":"23002#50.50.50.201","sip_name":"23002","user_id":8847075,"locked":false,"created":null,"h323_name":"1102","city":null,"email":null,"phone":null,"zip":null},{"address":null,"name":null,"country":null,"password":"****","extension":"1114","sip_id":"23003#50.50.50.201","sip_name":"23003","user_id":3680630,"locked":false,"created":null,"h323_name":"1103","city":null,"email":null,"phone":null,"zip":null},{"address":null,"name":null,"country":null,"password":"****","extension":"1115","sip_id":"23004#50.50.50.201","sip_name":"23004","user_id":136391,"locked":false,"created":null,"h323_name":"1104","city":null,"email":null,"phone":null,"zip":null},{"address":null,"name":null,"country":null,"password":"****","extension":"1116","sip_id":"23005#50.50.50.201","sip_name":"23005","user_id":5692227,"locked":false,"created":null,"h323_name":"1105","city":null,"email":null,"phone":null,"zip":null},{"address":null,"name":null,"country":null,"password":"****","extension":"1117","sip_id":"23006#50.50.50.201","sip_name":"23006","user_id":7559026,"locked":false,"created":null,"h323_name":"1106","city":null,"email":null,"phone":null,"zip":null},{"address":null,"name":null,"country":null,"password":"****","extension":"1118","sip_id":"23007#50.50.50.201","sip_name":"23007","user_id":3226075,"locked":false,"created":null,"h323_name":"1107","city":null,"email":null,"phone":null,"zip":null},{"address":null,"name":null,"country":null,"password":"****","extension":"1119","sip_id":"23008#50.50.50.201","sip_name":"23008","user_id":6184875,"locked":false,"created":null,"h323_name":"1108","city":null,"email":null,"phone":null,"zip":null},{"address":null,"name":null,"country":null,"password":"****","extension":"1120","sip_id":"23009#50.50.50.201","sip_name":"23009","user_id":1711112,"locked":false,"created":null,"h323_name":"1109","city":null,"email":null,"phone":null,"zip":null},{"address":null,"name":null,"country":null,"password":"****","extension":"1121","sip_id":"23010#50.50.50.201","sip_name":"23010","user_id":5383101,"locked":false,"created":null,"h323_name":"1110","city":null,"email":null,"phone":null,"zip":null}]}'
params = result[result.find('{"'):]
#print params
abc = json.loads(params)
# Here I'm trying to extract just sip_id from the string result using json.
# I am not able to figure out what went wrong .
for user_dict in abc['user_account']['sip_id']:
print 'usersname : %s' % (user_dict['sip_id'])
Change
for user_dict in abc['user_account']['sip_id']:
to
for user_dict in abc['user_account']:
abc['user_account'] is supposed to be a list.
A list a=['hello',1,2,'people'] has 4 elements accessed by indexes a[0] through a[3].
A dictionary d={'a':1,'b':2} has keys and values. In this case d['a'] has value 1, d[b'] has 2.
If you want to access something in abc['user_account'], type abc['user_account'][42]. If you want to iterate over it
for a in abc['user_account']:
print a