Answer the question
In order to leave comments, you need to log in
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": ""
}
}
]
}
list = [ ["regNum", 777], ["maxPrice", 950], ["number", "1"], ["number", "2"], ["name", "Apples"], ["name", "Bananas"] ]
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question