从两个列表中创建json,以在Javascript中生成堆叠条形图

从两个列表中创建json,以在Javascript中生成堆叠条形图,javascript,python,json,dictionary,plotly,Javascript,Python,Json,Dictionary,Plotly,从两个列表中创建json,以在Javascript中生成堆叠条形图 list1 = ['2019-03-05', '2019-02-20', '2019-02-20', '2019-02-19', '2019-02-18', '2019-02-16', '2019-02-13', '2019-02-12', '2019-02-12', '2019-02-11', '2019-02-08', '2019-02-07', '2019-02-06', '2019-02-05', '2019-02-05

从两个列表中创建json,以在Javascript中生成堆叠条形图

list1 = ['2019-03-05', '2019-02-20', '2019-02-20', '2019-02-19', '2019-02-18', '2019-02-16', '2019-02-13', '2019-02-12', '2019-02-12', '2019-02-11', '2019-02-08', '2019-02-07', '2019-02-06', '2019-02-05', '2019-02-05', '2019-02-02', '2019-02-02', '2019-01-29', '2019-01-28', '2019-01-24', '2019-01-24', '2019-01-24', '2019-01-23', '2019-01-16', '2019-01-16', '2019-01-14', '2019-01-10', '2019-01-10', '2019-01-09', '2019-01-06', '2019-01-05', '2019-01-05', '2019-01-01']
list2 =['Type Error', 'Type Error', 'Type Error', 'Type Error', 'Segmenatation', 'Type Error', 'Type Error', 'Heap  Failure', 'Heap  Failure', 'Type Error', 'I/O Error', 'Type Error', 'Type2 Error', 'Type Error', 'Type Error', 'Type Error', 'Heap  Failure', 'Type2 Error', 'Heap I/O', 'CPU  Recovery Error', 'Type Error', 'Type Error', 'CPU  Recovery Error', 'CPU  Recovery Error', 'Heap I/O', 'Type2 Error', 'Heap  Failure', 'I/O Error', 'Heap I/O', 'Type Error', 'Type2 Error', 'Type Error', 'Type Error']
由此,我需要在下面提供的链接中绘制图表

我尝试使用默认的dict并获取单键多值,但是链接中的json却大不相同

请帮帮我

试过这个,但没有结果

from collections import defaultdict
v = defaultdict(list)
for i in range(len(list1)):
  v[list1[i]].append(list2[i])

对于堆叠条形图,每个数据类型都在单独的轨迹中。在您的情况下,您需要为每种错误类型创建一个条形图(假设您希望x轴上的日期和y轴是错误计数)

对于小的列表,您可以在其上循环并搜索匹配项。对于更大的数据集,panda(或类似的东西)应该更高效、更易于使用

import collections
import plotly

list1 = ['2019-03-05', '2019-02-20', '2019-02-20', '2019-02-19', '2019-02-18', '2019-02-16', '2019-02-13', '2019-02-12', '2019-02-12', '2019-02-11', '2019-02-08', '2019-02-07', '2019-02-06', '2019-02-05', '2019-02-05', '2019-02-02', '2019-02-02', '2019-01-29', '2019-01-28', '2019-01-24', '2019-01-24', '2019-01-24', '2019-01-23', '2019-01-16', '2019-01-16', '2019-01-14', '2019-01-10', '2019-01-10', '2019-01-09', '2019-01-06', '2019-01-05', '2019-01-05', '2019-01-01']
list2 =['Type Error', 'Type Error', 'Type Error', 'Type Error', 'Segmenatation', 'Type Error', 'Type Error', 'Heap  Failure', 'Heap  Failure', 'Type Error', 'I/O Error', 'Type Error', 'Type2 Error', 'Type Error', 'Type Error', 'Type Error', 'Heap  Failure', 'Type2 Error', 'Heap I/O', 'CPU  Recovery Error', 'Type Error', 'Type Error', 'CPU  Recovery Error', 'CPU  Recovery Error', 'Heap I/O', 'Type2 Error', 'Heap  Failure', 'I/O Error', 'Heap I/O', 'Type Error', 'Type2 Error', 'Type Error', 'Type Error']

assert len(list1) == len(list2)

traces = []

# get a set of unique errors
for error in sorted(list(set(list2))):
    errors = collections.defaultdict(int)

    # check this particular error occurred on this date
    for ix, d in enumerate(list1):
        if list2[ix] == error:
            errors[d] += 1

    traces.append(plotly.graph_objs.Bar(x=list(errors.keys()),
                                        y=list(errors.values()),
                                        name=error))

layout = plotly.graph_objs.Layout(
    barmode='stack'
)
fig = plotly.graph_objs.Figure(data=traces, layout=layout)

plotly.offline.plot(fig)

对于堆叠条形图,每个数据类型都需要在单独的轨迹中。在您的情况下,您需要为每种错误类型创建一个条形图(假设您希望x轴上的日期和y轴是错误计数)

对于小的列表,您可以在其上循环并搜索匹配项。对于更大的数据集,panda(或类似的东西)应该更高效、更易于使用

import collections
import plotly

list1 = ['2019-03-05', '2019-02-20', '2019-02-20', '2019-02-19', '2019-02-18', '2019-02-16', '2019-02-13', '2019-02-12', '2019-02-12', '2019-02-11', '2019-02-08', '2019-02-07', '2019-02-06', '2019-02-05', '2019-02-05', '2019-02-02', '2019-02-02', '2019-01-29', '2019-01-28', '2019-01-24', '2019-01-24', '2019-01-24', '2019-01-23', '2019-01-16', '2019-01-16', '2019-01-14', '2019-01-10', '2019-01-10', '2019-01-09', '2019-01-06', '2019-01-05', '2019-01-05', '2019-01-01']
list2 =['Type Error', 'Type Error', 'Type Error', 'Type Error', 'Segmenatation', 'Type Error', 'Type Error', 'Heap  Failure', 'Heap  Failure', 'Type Error', 'I/O Error', 'Type Error', 'Type2 Error', 'Type Error', 'Type Error', 'Type Error', 'Heap  Failure', 'Type2 Error', 'Heap I/O', 'CPU  Recovery Error', 'Type Error', 'Type Error', 'CPU  Recovery Error', 'CPU  Recovery Error', 'Heap I/O', 'Type2 Error', 'Heap  Failure', 'I/O Error', 'Heap I/O', 'Type Error', 'Type2 Error', 'Type Error', 'Type Error']

assert len(list1) == len(list2)

traces = []

# get a set of unique errors
for error in sorted(list(set(list2))):
    errors = collections.defaultdict(int)

    # check this particular error occurred on this date
    for ix, d in enumerate(list1):
        if list2[ix] == error:
            errors[d] += 1

    traces.append(plotly.graph_objs.Bar(x=list(errors.keys()),
                                        y=list(errors.values()),
                                        name=error))

layout = plotly.graph_objs.Layout(
    barmode='stack'
)
fig = plotly.graph_objs.Figure(data=traces, layout=layout)

plotly.offline.plot(fig)

将列表压缩在一起:
v=dict(Zip(list1,list2))
@zwer它不起作用,因为我在列表1中只有24个唯一的日期,总计数是33。因为dict不能有重复的键,上面的函数不能将列表压缩在一起:
v=dict(zip(list1,list2))
@zwer它不能工作,因为我在list1中只有24个唯一的日期,总计数是33。由于dict不能有重复的键,上述功能无法工作感谢@maximilian。这就像一个职业选手。荣誉:)谢谢@maximilian。这就像一个职业选手。荣誉:)