繁体   English   中英

计算Python字典列表中具有相同键的元素数

[英]Counting the number of elements having the same keys in a Python list of dictionaries

我有以下字典的python列表

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}
]

我有seq_count作为不同'sequence_id'num_classes的总数作为不同'au_type'的总数。 在上面的例子中:

seq_count = 2
num_classes = 2

我需要实现一个列表numid_seq_cl[seq_count][num_classes]返回具有相同'au_type''sequence_id'的不同'id'的数量。 在上面的例子中,

numid_seq_cl[0][1] = 3
numid_seq_cl[0][3] = 1
numid_seq_cl[1][1] = 3
numid_seq_cl[1][3] = 3

以下是列表的解决方案:

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)

输出:

[[0, 3, 0, 1], [0, 3, 0, 3]]

但是,我强烈建议不要使用列表,而是使用字典:

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

编辑:添加列表解决方案

在你的案件中一个有意义的结果将是一个dicts的词典。
对灵活的collections.defaultdict对象使用以下方法:

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}}

这是你期望的索引

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM