#๐Ÿ”’ Help with dictionaries

56 messages ยท Page 1 of 1 (latest)

humble turret
#

Hey I've a doubt in implementing some logic in the code

wheat nymphBOT
#

@humble turret

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.

humble turret
#

i've a string

json_string = "{\"day 1\":  \"location 1\",\n\"day 2\": \"location 2\",\n\"day 3\": [\"location 3\",\"location4\"],\n\"day 4\":[\"location 5\",\"location 6\"]}"

and I want to convert this into a list which will have 4 dict

{"day 1": "location 1", "day 2": "location 2", "day 3": "location 3", "day 4": "location 5",}
{"day 1": "location 1", "day 2": "location 2", "day 3": "location 3", "day 4": "location 6"}
{"day 1": "location 1", "day 2": "location 2", "day 3":  "location4", "day 4": "location 5"}
{"day 1": "location 1", "day 2": "location 2", "day 3": "location4", "day 4": "location 6"}```
#

implemented code byfar -


import json
json_string = "{\"day 1\":  \"location 1\",\n\"day 2\": \"location 2\",\n\"day 3\": [\"location 3\",\"location4\"],\n\"day 4\":[\"location 5\",\"location 6\"]}"

day_plans = json.loads(json_string)

print(day_plans['day 1'])

result_dicts = []
for key, value in day_plans.items():
    if isinstance(value, list):
        for location in value:
            new_dict = day_plans.copy()
            new_dict[key] = location
            result_dicts.append(new_dict)

for data in result_dicts:
    print(data)```
brazen forum
#

Hey, first i would advise you to use lists no matter what, even if its only on location

#

Then you iterate through the values of the dictionary and you get the longer one

#

Not sure why do you want to have a list with 4 dict?

brazen forum
humble turret
#

This is my internship code
I want to first convert this into 4 to show users 4 different output of what choices he have

humble turret
brazen forum
#

Got it you want to print all the possibilities right ?

humble turret
#

check now please

brazen forum
#

Okay

stray path
#

do you always have at most 4 options, or do you need a more general solution to handle an arbitrary number of arrays of arbitrary length

humble turret
#

current output

{'day 1': 'location 1', 'day 2': 'location 2', 'day 3': 'location 3', 'day 4': ['location 5', 'location 6']}
{'day 1': 'location 1', 'day 2': 'location 2', 'day 3': 'location4', 'day 4': ['location 5', 'location 6']}
{'day 1': 'location 1', 'day 2': 'location 2', 'day 3': ['location 3', 'location4'], 'day 4': 'location 5'}
{'day 1': 'location 1', 'day 2': 'location 2', 'day 3': ['location 3', 'location4'], 'day 4': 'location 6'}```
humble turret
#

or 8

#

other sample string
"{"day 1": "location 1",\n"day 2": "location 2",\n"day 3": ["location 3","location4"],\n"day 4":"location 5"} "

deft geyser
humble turret
stray path
#

this is a pretty common leetcode style-question

humble turret
#

this is the api output i was provided

stray path
#

you can solve this either iteratively or recurisvely

deft geyser
humble turret
stray path
#

that's just how json looks when it's stringified

#

you can remove the escapes if you use single quotes for the outer string

humble turret
#

I just use json.loads

humble turret
stray path
#

your solution is pretty close to the iterative case, but it's missing a loop over result_dicts

humble turret
#

I did iterative approach

stray path
#

every time you have an array of values, you need to duplicate the elements in result_dicts

humble turret
#

50%

deft geyser
#

ima let connor handle this actually

humble turret
stray path
#

in pseudocode this might look something like

#
# we initialize the list to empty dict to make iteration simpler
+ result_dicts = [{}]
for key, value in day_plans.items():
    if isinstance(value, list):
# we keep a copy of result_dicts and change the array to be empty to simplify appending
+       copy = result_dicts
+       result_dicts = []
        for location in value:
+           for r in copy:
+              new_dict = r.copy()
               new_dict[key] = location
               result_dicts.append(new_dict)
# no array means we add the key to every dictionary
+   else:
+     for r in result_dicts:
+       r[key] = value
#

disclaimer i haven't run this, just wrote in discord. this is roughly what you want for the iterative approach

#

at each step you're effectively multiplying the number of dictionaries in the result_dicts array by the number of elements in the value

#
  • added on changed lines for effect and i'd remove comments in production code
humble turret
#

wow

#

It ran

#

damn thanks dude

#

๐Ÿ˜ญ

stray path
humble turret
#

you've no idea how much this helped

#

do you code alot?

stray path
#

make sure you're able to write it without looking at this example! i didn't mean to solve the problem haha

humble turret
#

god damn

#

744 contrib

#

I just followed you

wheat nymphBOT
#
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.