检查字典数组中的键值对,并相应地更新另一个列表 [英] Check arrays of dictionaries for key-value pair and update another list accordingly

查看:82
本文介绍了检查字典数组中的键值对,并相应地更新另一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个包含字典的数组.我想检查这些数组并根据迭代数组中的字典时遇到的键值对更新另一个列表.

因此,对于以下4个情感数组:

senti_array1 = [{'senti':'Positive', 'count':15}, {'senti':'Negative', 'count':10}, {'senti':'Neutral', 'count':5}]
senti_array2 = [{'senti':'Positive', 'count':8}, {'senti':'Negative', 'count':4}]
senti_array3 = [{'senti':'Positive', 'count':2}]
senti_array4 = [{'senti':'Negative', 'count':7}, {'senti':'Neutral', 'count':12}]

pos_list=[]
neg_list=[]
neu_list=[]

如果它们是负面情绪,则在这种情况下应使用其计数值更新相应的列表(neg_list),否则,如果数组中不存在``负面''情绪,则应在列表中附加0. /p>

最终输出应为:

pos_list=[15, 8, 2, 0]
neg_list=[10, 4, 0, 7]
neu_list=[5, 0, 0, 12]

我尝试使用正常的for循环,但是我没有得到期望的输出,因为如果没有其他条件,则每次检查else条件时,列表中都会附加一个0,这会产生错误的输出.我认为可以使用map或lambda函数,但不知道如何开始.

解决方案

您可以创建一个dict,将情感映射到数组索引到计数的dict映射,以便您可以遍历3个情感,并通过用于构建计数列表的数组的数量范围.使用dict.get方法将默认计数设置为0:

mapping = {}
for i, l in enumerate((senti_array1, senti_array2, senti_array3, senti_array4)):
    for d in l:
        mapping.setdefault(d['senti'], {})[i] = d['count']
pos_list, neg_list, neu_list = ([mapping.get(s, {}).get(k, 0) for k in range(i + 1)] for s in ('Positive', 'Negative', 'Neutral'))

给出示例输入后,pos_list变为:

[15, 8, 2, 0]

neg_list变为:

[10, 4, 0, 7]

neu_list变为:

[5, 0, 0, 12]

I have multiple arrays containing dictionaries. I want to check these arrays and update another list as per the key-value pairs encountered while iterating the dictionaries within arrays.

So for following 4 sentiment arrays:

senti_array1 = [{'senti':'Positive', 'count':15}, {'senti':'Negative', 'count':10}, {'senti':'Neutral', 'count':5}]
senti_array2 = [{'senti':'Positive', 'count':8}, {'senti':'Negative', 'count':4}]
senti_array3 = [{'senti':'Positive', 'count':2}]
senti_array4 = [{'senti':'Negative', 'count':7}, {'senti':'Neutral', 'count':12}]

pos_list=[]
neg_list=[]
neu_list=[]

If their is a negative sentiment present the corresponding list (neg_list) in this case should be updated with its count value, else 0 should be appended in the list if 'Negative' sentiment is not present in the array.

The final output should be :

pos_list=[15, 8, 2, 0]
neg_list=[10, 4, 0, 7]
neu_list=[5, 0, 0, 12]

I have tried using normal for loops but I do not get the desired output because each time if else condition is checked a 0 is appended in the list if sentiment not present, which yields wrong output. I think maps or lambda functions can be used for this but have no idea how to start.

解决方案

You can create a dict that maps sentiments to a dict mapping of array indices to counts, so that you can iterate through the 3 sentiments and iterate an index through the range of the number of arrays to build lists of counts. Use the dict.get method set the default count to 0:

mapping = {}
for i, l in enumerate((senti_array1, senti_array2, senti_array3, senti_array4)):
    for d in l:
        mapping.setdefault(d['senti'], {})[i] = d['count']
pos_list, neg_list, neu_list = ([mapping.get(s, {}).get(k, 0) for k in range(i + 1)] for s in ('Positive', 'Negative', 'Neutral'))

Given your sample input, pos_list becomes:

[15, 8, 2, 0]

neg_list becomes:

[10, 4, 0, 7]

and neu_list becomes:

[5, 0, 0, 12]

这篇关于检查字典数组中的键值对,并相应地更新另一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆