I have the following python list of dictionaries
list_of_dict = [
{'id': 0, 'au_type': 1, 'sequence_id': 0, 'AU_start_position': 0},
{'id': 1, 'au_type': 1, 'sequence_id': 0, 'AU_start_position': 4095},
{'id': 2, 'au_type': 1, 'sequence_id': 0, 'AU_start_position': 8092},
{'id': 0, 'au_type': 3, 'sequence_id': 0, 'AU_start_position': 5678},
{'id': 0, 'au_type': 1, 'sequence_id': 1, 'AU_start_position': 13525},
{'id': 1, 'au_type': 1, 'sequence_id': 1, 'AU_start_position': 13587},
{'id': 2, 'au_type': 1, 'sequence_id': 1, 'AU_start_position': 14576},
{'id': 0, 'au_type': 3, 'sequence_id': 1, 'AU_start_position': 15019},
{'id': 1, 'au_type': 3, 'sequence_id': 1, 'AU_start_position': 15560},
{'id': 2, 'au_type': 3, 'sequence_id': 1, 'AU_start_position': 16004}
]
I have seq_count
as the total number of different 'sequence_id'
and num_classes
as the total number of different 'au_type'
. In the example above:
seq_count = 2
num_classes = 2
I need to implement a list numid_seq_cl[seq_count][num_classes]
returning the number of different 'id'
with the same 'au_type'
and 'sequence_id'
. In the example above,
numid_seq_cl[0][1] = 3
numid_seq_cl[0][3] = 1
numid_seq_cl[1][1] = 3
numid_seq_cl[1][3] = 3
Here is the solution with lists:
max_au = -1
max_sq = -1
# Find list limits
for item in list_of_dict:
sq = item['sequence_id']
max_au = max(max_au, item['au_type'])
max_sq = max(max_sq, item['sequence_id'])
assert max_au > 0, max_sq >= 0
numid_seq_cl = [[0 for j in range(max_au + 1)] for i in range(max_sq + 1)]
# Fill list
for item in list_of_dict:
numid_seq_cl[item['sequence_id']][item['au_type']] += 1
print(numid_seq_cl)
Output:
[[0, 3, 0, 1], [0, 3, 0, 3]]
However, I strongly recommend not using a list, but using a dictionary:
numid_seq_cl = {}
for item in list_of_dict:
au = item['au_type']
sq = item['sequence_id']
# Set default values, so dictionary items can be easily incremented
if au not in numid_seq_cl: # Set a default value for the first level of the dictionary
numid_seq_cl[au] = {}
if sq not in numid_seq_cl[au]: # Set a default value for the second level of the dictionary
numid_seq_cl[au][sq] = 0
numid_seq_cl[au][sq] += 1
EDIT: Added list solution
A meaningful result in your case would be a dict of dicts.
Use the following approach with flexible collections.defaultdict
object:
from collections import defaultdict
list_of_dicts = [
{'id': 0, 'au_type': 1, 'sequence_id': 0, 'AU_start_position': 0},
{'id': 1, 'au_type': 1, 'sequence_id': 0, 'AU_start_position': 4095},
{'id': 2, 'au_type': 1, 'sequence_id': 0, 'AU_start_position': 8092},
{'id': 0, 'au_type': 3, 'sequence_id': 0, 'AU_start_position': 5678},
{'id': 0, 'au_type': 1, 'sequence_id': 1, 'AU_start_position': 13525},
{'id': 1, 'au_type': 1, 'sequence_id': 1, 'AU_start_position': 13587},
{'id': 2, 'au_type': 1, 'sequence_id': 1, 'AU_start_position': 14576},
{'id': 0, 'au_type': 3, 'sequence_id': 1, 'AU_start_position': 15019},
{'id': 1, 'au_type': 3, 'sequence_id': 1, 'AU_start_position': 15560},
{'id': 2, 'au_type': 3, 'sequence_id': 1, 'AU_start_position': 16004}
]
numid_seq_cl = defaultdict(lambda : defaultdict(int)) # default structure
for d in list_of_dicts:
numid_seq_cl[d['sequence_id']][d['au_type']] += 1
numid_seq_cl = {k: dict(v) for k, v in numid_seq_cl.items()}
print(numid_seq_cl) # {0: {1: 3, 3: 1}, 1: {1: 3, 3: 3}}
And here's your expected indexing :
print(numid_seq_cl[0][1]) # 3
print(numid_seq_cl[0][3]) # 1
print(numid_seq_cl[1][1]) # 3
print(numid_seq_cl[1][3]) # 3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.