合并一些列表并将它们重新组合到一个字典中,以绘制多序列的Highcharts

2024-05-17 19:44:30 发布

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

animal = ['Hamster', 'Dolphin', 'Ant', 'Hamster', 'Hamster', 'Ant']
date = [20151007, 20151007, 20151007, 20141007, 20120101, 20010101]
total = [27, 5, 5, 5, 18, 40]

我想把这三张单子合并成一张。但如果动物是相同的,它应该分组在一起,总数应该遵循asc日期。如果动物类型没有足够的日期,它将变为0。例如:

[{'name': 'Hamster', 'data': [0, 18, 5, 27]}, 
{'name': 'Dolphin', 'data': [0, 0, 0, 5]}, 
{'name': 'Ant', 'data': [40, 0, 0, 5]}]

我正试着把它们画成图表 日期在它自己的列表中。你知道吗

[20010101, 20120101, 20141007, 20151007] 

我可以使用以下方法将列表组合在一起:

zip(animal, date, total)

但如何让他们进入自己的类型和安排的总asc日期?你知道吗

编辑

我就是这样走到现在的。 我觉得效率不高。 你怎么认为?有什么改进建议吗?(仅限Python建议)

b = []
for k, v in enumerate(animal):
    counter = 0
    for i in range(len(b)):
        try:
            if b[i]['name'] == v:
                b[i]['data'].append((date[k], total[k]))
                counter = counter + 1
        except KeyError:
            continue
    if counter == 0:
        b.append({'name': v,  'data': [(date[k], total[k])]})

test = set(date)

for k, v in enumerate(list(OrderedDict.fromkeys(animal))):
    for x in test:
        try:
            for i in range(len(test)):
                if b[k]['data'][i][0] == x:
                    break
        except IndexError:
            b[k]['data'].append((x, 0))
    b[k]['data'].sort(key=itemgetter(0))
    temp = [x[1] for x in b[k]['data']]
    b[k]['data'] = []
    b[k]['data'] = temp

结果:

[{'data': [0, 18, 5, 27], 'name': 'Hamster'}, 
{'data': [0, 0, 0, 5], 'name': 'Dolphin'}, 
{'data': [40, 0, 0, 5], 'name': 'Ant'}]

Tags: nameintestfordatadateifcounter
1条回答
网友
1楼 · 发布于 2024-05-17 19:44:30

我想你想要这样的东西:

animals = ['Hamster', 'Dolphin', 'Ant', 'Hamster', 'Hamster', 'Ant']
dates = [20151007, 20151007, 20151007, 20141007, 20120101, 20010101]
totals = [27, 5, 5, 5, 18, 40]
readings = list(zip(animals, dates, totals))
dates = set(dates)
data = dict()
for animal in animals:
    data[animal] =  {(date, total) for (x, date, total) in readings if x == animal}
    missingDates = dates - {d[0] for d in data[animal] }
    data[animal] |= {(date, 0) for date in missingDates} 

此时,data

{'海豚':{(20120101,0),(20151007,5),(20141007,0),(20010101,0)},'仓鼠':{(20151007,27),(20141007,5),(20120101,18),(20010101,0)},'蚂蚁':{(20120101,0),(20151007,5),(20141007,0),(20010101,40)}

你只需要把它分开,按日期对每只动物的数据进行排序。我希望这有帮助。你知道吗

这是我答应的进一步解释。很遗憾,我无法同时看到您的评论和编辑屏幕;我希望我能回答您提出的所有问题。将三个列表压缩到一起后,我们需要提取每种动物的数据。右边的表达式

data[animal] =  {(date, total) for (x, date, total) in readings if x == animal}

是一个集合理解,它给了我们一个特定动物相关的所有(日期,总数)对的集合。现在我们需要找到丢失的日期。 我使用了设置差异:

missingDates = dates - {d[0] for d in data[animal] }  

这就是我使用set的原因:能够简洁地编写缺少日期的表达式。相反,我可以使用列表:

data[animal] = [(date, total) for (x, date, total) in readings if x == animal]
missingDates = [d for d in dates if d not in [d[0] for d in data[animal]]

如果我这么做了,我就不需要更早的声明了

dates = set(dates) 

但是我想避免用“missingDates”这个复杂的表达,我也不想写

presentDates = [d[0] for d in data[animal] 
missingDates = [d for d in dates if d not in presentDates]

现在,我必须为缺少的日期添加对(date,0)。如果我用列表,我会写

data[animal] += [(date, 0) for date in missingDates] 

但是没有为集合定义+操作;我需要使用union。(两个集合的并集是至少属于其中一个集合的元素集合。)并集运算表示为|。正确的是,它可以表示两个整数的位或,但也可以表示其他操作,就像+可能意味着数字的相加或列表的串联一样。所以:

data[animal] |= {(date, 0) for date in missingDates}

如果你对集合不满意,一定要用列表代替,但是你应该注意学习集合。它们非常有用。你知道吗

为了继续我之前提到的观点,您现在需要按日期对数据[动物]进行排序。在这里,我们将不得不改回列表,因为集合是未排序的。你知道吗

data[animal] = list(data[animal])
data[animal].sort()
data[animal] = [d[1] for d in data[animal]]

当然,如果您选择在整个过程中使用列表,则不需要这些行中的第一行。你知道吗

我希望这能帮你澄清。如果你还有其他问题,请告诉我。你知道吗

相关问题 更多 >