Python question


#1

Hello,

This Python question is not from code academy but I’m looking for some help on this question.

Would appreciate any help or advice:

QUESTION:

(1) Create a function to transform a string structure like this:

text="key1=value1;key2=value2\nkeyA=valueA\n..."

into a Python dictionary like this:

             a[0]["key1"]="value1"  
             a[0]["key2"]="value2"  
             a[1]["keyA"]="valueA"  

Realize that ‘;’ separates items within the same dictionary index while ‘\n’ separates items on a different index.

(2) Create another function that transforms the python dictionary back to the string structure.

Appreciate any advice or help.

Thank you,
Zhiwei.


#2

you could start by creating the list.

then use .split() to split the the items of different indexes (separated by \n).

then loop over the items of different indexes, within the loop create another list (index specific list). Then .split() the items within the same index, make the dictionaries and append them to the index specific list

after having dealt with each index specific list, append them to the result list (the list i mention in first paragraph of this reply)