Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python dictionary array [closed]

I would like to ask about a particular problem, I have encountered lately.

I have list containing items e.g.

list1 = ['library','book','room','author','description','genre','publish_date','price','title']

and dictionary containing keys and values, keys are items from list1 and values are its childs,e.g.

dictionary1 = {'room': ['book'], 'title': [], 'price': [], 'author': [], 'library': [ 'room', 'book'], 'book': ['author', 'title', 'genre', 'price', 'publish_date', 'description'], 'publish_date': [], 'genre': [], 'description': []}

Basically what i want to do is to go through items in dictionary1 and if value of certain key is also key with values, i want to add values of the value to the key.

For example:

'library': ['room','book']

book contains author, title, genre, price, publishdate, description.

And I want to add all these items to library key, so it would look like:

'library': ['room','book','author', 'title', 'genre', 'price', 'publish_date', 'description'], 'publish_date': [], 'genre': [], 'description': []] 
like image 778
Daniel Javorský Avatar asked Nov 28 '25 03:11

Daniel Javorský


1 Answers

Pseudocode:

dictionary = ...//your input
dictionaryOut={}

list=[]

for key, value in dictionary
    dictionaryOut[key] = copy(value)

    if length(value[0]):
        list=[value[0]]

    while not empty(list):
        list.append(dictionary(list[0]))
        dictionaryOut[key].append(dictionary(list.pop(0))

This should do it as long as we are talking python and appending to value will actually update the list in your dictionary.

like image 115
ted Avatar answered Nov 30 '25 17:11

ted



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!