根据第二个lis中的位数对列表项进行分组

2024-10-06 16:16:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我们的目标是创建一个堆叠的条形图,显示推特(我从tweepy那里得到的)在360秒(每秒)内的情绪。我有两张单子。第一个是按时间顺序对推文进行情感分析,第二个是按时间顺序对每秒推文的数量进行分析

list1 = ("neg", "pos", "pos", "neu", "neg", "pos", "neu", "neu",...)
list2 = (2, 1, 3, 2,...)

现在,我想创建某种嵌套循环,并使用list2来计算列表1中的项目。然后,我将有3个列表,其中每个情绪都有360个值,可以用于图表。它应该给我一个类似的输出:

lis_negative = (1, 0, 1, 0, ...)
lis_positive = (1, 1, 1, 0, ...)
lis_neutral = (0, 0, 1, 2, ...)

我如何创建这个循环?是否有更简单的方法?除了matplotlib之外,我更愿意不使用任何库


Tags: pos目标列表数量顺序时间情感单子
1条回答
网友
1楼 · 发布于 2024-10-06 16:16:16

代码:

from itertools import islice
from collections import Counter

def categorize(clas, amounts):
    cats = {'neg': [], 'pos': [], 'neu': []}
    clas = iter(clas)

    for a in amounts:
        cs = Counter(islice(clas, a)) # take a items
        for cat in cats:
            cats[cat].append(cs[cat])
    return cats

演示:

>>> t1 = ('neg', 'pos', 'pos', 'neu', 'neg', 'pos', 'neu', 'neu')
>>> t2 =  (2, 1, 3, 2)
>>> 
>>> categorize(t1, t2)
{'neg': [1, 0, 1, 0], 'neu': [0, 0, 1, 2], 'pos': [1, 1, 1, 0]}

根据要求,无导入的解决方案:

def make_counter(iterable):
    c = {}
    for x in iterable:
        c[x] = c.get(x, 0) + 1
    return c

def categorize(clas, amounts):
    cats = {'neg': [], 'pos': [], 'neu': []}
    pos = 0

    for a in amounts:
        chunk = clas[pos:pos+a]
        pos += a
        cs = make_counter(chunk)
        for cat in cats:
            cats[cat].append(cs.get(cat, 0))
    return cats

编辑:更短导入更少解决方案:

def categorize(clas, amounts):
    cats = {k:[0]*len(amounts) for k in ('neg', 'pos', 'neu')}
    pos = 0

    for i, a in enumerate(amounts):
        chunk = clas[pos:pos+a]
        pos += a
        for c in chunk:
            cats[c][i] += 1

    return cats

相关问题 更多 >