#๐Ÿ”’ Parsing dict like structure to dict

15 messages ยท Page 1 of 1 (latest)

winter totem
heavy krakenBOT
#

@winter totem

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

restive storm
winter totem
#

Ok. I will modify that part. However I am not able to find any logic to parse the list indices.

restive storm
#

Actually, I take that back, recursion wouldn't work well here, because there are multiple lines that define what a.b.c[0] is, so it can't just be resolved in a recursive call.

#

The key name stops when you reach a . or [. Then depending on whether it is a . or [ you'd create a subdictionary or sublist.

restive storm
winter totem
#

Alright.
Can you provide a snippet to parse that part ?

restive storm
#

Doing this with recursion, you only need to deal with the top most element and not all the parts. So there should be some call of this function (when handling a.b.c) where it gets a dictionary, but all the keys start with [, so you'll return a list back in that call.

winter totem
#

I just cant get my head around this, looking at it for a long time that nothing comes to my mind.

restive storm
#

When you don't have a [ start, you should gather all the a. together and pass them to a single call of your convert function so that that call can return a complete a dictionary

#

Another approach would be to replace all the [ with . and ignore the ] and then just make everything dictionaries and then at some point in the process look for dictionaries whose keys are all numeric and change those to lists.

heavy krakenBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.