B
B
beduin012020-08-28 15:10:10
Python
beduin01, 2020-08-28 15:10:10

How to fill the structure with data?

I have a data structure in the following format. It can be thought of as a set of dictionaries or something similar. It's not essential.

data = {
  "regNum": "",
  "maxPrice": "",
  "lots": [{
      "number": "",
      "objects": {
        "name": ""
      }
    }, {
      "number": "",
      "objects": {
        "name": ""
      }
    }
    ]
  }


I need to fill this structure with data:
list = [ ["regNum", 777], ["maxPrice", 950], ["number", "1"], ["number", "2"], ["name", "Apples"], ["name", "Bananas"] ]

I started walking on it through recursion and got stuck. As a result, I either get all the name Apples, then all "Bananas".

The language doesn't really matter. Syntax adjusted to Python-ovsky.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
PavelMos, 2020-08-29
@PavelMos

In this list, different data needs to be parsed in different ways. If the key: value regNum and maxPrice can be taken when iterating over i - if i[0]==regNum then write data['regNum']=i[1], then you need to correlate the number in number and the name in name in their order. Somehow separately select all i elements that have i[0]=='number', then all those that have i[0]=='name', then go through the two lists

list1=[1,2,3]
list2=['Apples','Bananas','Oranges']
list3=list(zip(list1,list2))
#[(1,'Apples'),(2,'Bananas')...]
#сформировать список для lots
lots=[{'number':i[0],'objects':{'name':i[1]}} for i in list3]
#добавить список как элемент
data['lots']=lots
Out[353]: 
[{'number': 1, 'objects': {'name': 'Apples'}},
 {'number': 2, 'objects': {'name': 'Bananas'}},
 {'number': 3, 'objects': {'name': 'Oranges'}}]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question