如何将两个列表转换为json格式

时间:2019-04-16 07:20:31

标签: python json python-3.x flask deep-learning

我无法将两个列表转换为json格式。

我试图将两个列表转换成字典格式,然后转换成json格式,但是那里也没有重复的键。

    p = np.argmax(y[:416], axis=-1)
    # print(p)
    flat_list_te = padd_to_2d_senti[:416]
    flat_list_test = [item for sublist in flat_list_te for item in sublist]
    # print(flat_list_test)
    # flat_list = [item for sublist in y_te[i] for item in sublist]
    flat_list_pred = [item for sublist in p for item in sublist]
    key = []
    value = []
    for w,pred in zip(flat_list_test,flat_list_pred):
        predicted_tag = idx2tag[pred]

        if predicted_tag !='O':
            a.append(w)
            b.append(idx2tag[pred])
    test_pred = dict(zip(key,value))
expected output:
    key = ["phone","age","class","class"]
    value = [123,4,5,6]
    dic = {"phone":123,"age":4,"class":5,"class":6}

1 个答案:

答案 0 :(得分:2)

dict中的每个键都必须唯一。所以这本字典是不可能的:

dic = {"phone":123,"age":4,"class":5,"class":6}

"class"存在两次。

可能的解决方案:

  • "class"字符串之一重命名为唯一字符串
  • 为键tuple的值创建一个"class",例如dic = {"class":(5, 6)}