Как добавить несколько элементов, используя один ключ в Python? - PullRequest
0 голосов
/ 13 мая 2019

Как добавить несколько ключей, используя один ключ в массиве?

def apidurusanalizi_func(self):
    isyerleri = data.iloc[:,0].values
    hatalar = np.delete(data.columns, 0)
    cluster1 = []
             for i,row in  enumerate(bc):
                   for j, y in enumerate(row):
                          if y:
                              isyeri = isyerleri[i]
                              hata = hatalar[j]
                              record = {"isyeri":str(isyeri), hata.lower().replace(" ", ""):hata}
                              if index == 0:
                                 cluster1.append(record)/*add record*/
    return HttpResponse(json.dumps({'cluster1': cluster1}, indent=2), content_type="application/json")

Вывод, как показано ниже:

    {
      "cluster1": [
        {
          "isyeri": "15400002",/*the same key*/
          "olcmeodasi": "OLCME ODASI"
        },
        {
          "isyeri": "15400002",/*the same key*/
          "tipdegayari": "TIP DEG AYARI"
        }
    }

Формат, который я хочу принять в формате JSON, как показано ниже:

{
  "cluster1": [
    {
      "isyeri": "15400002", /* the same value for two values */
      "olcmeodasi": "OLCME ODASI"
      "tipdegayari": "TIP DEG AYARI"
    }
}

1 Ответ

0 голосов
/ 13 мая 2019

Вы создаете кластер не как dict, а как список: cluster1 = [] и добавляете к нему позже: cluster1.append(record).Вы должны определить cluster1 как dict и установить значения записи.

Ваш код не является автономным и не может быть полностью протестирован, но это должно работать:

def apidurusanalizi_func(data):
    isyerleri = data.iloc[:,0].values
    hatalar = np.delete(data.columns, 0)
    cluster1 = {}
    for i,row in  enumerate(bc):
        for j, y in enumerate(row):
            if y:
                isyeri = isyerleri[i]
                hata = hatalar[j]
                if index == 0:
                    cluster1["isyeri"] = str(isyeri)
                    cluster1[hata.lower().replace(" ", "")] = hata
    return HttpResponse(
        json.dumps({'cluster1': cluster1}, indent=2),
        content_type="application/json"
    )
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...