#๐ Help with dictionaries
56 messages ยท Page 1 of 1 (latest)
@humble turret
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.
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)```
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?
Seems to me 2 of these items are duplicates
This is my internship code
I want to first convert this into 4 to show users 4 different output of what choices he have
oh sorry I just checked
wait let me edit it
Got it you want to print all the possibilities right ?
check now please
yes
Okay
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
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'}```
no not mandatory
There can be 2 options aswell
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"} "
something i see immediately is you can just have single quotes for the json string instead of a backslash after every double quote in the string
It's not me
I was given it this way
this is a pretty common leetcode style-question
this is the api output i was provided
you can solve this either iteratively or recurisvely
bit strange of a question to spam backslashes
Ikr
that's just how json looks when it's stringified
you can remove the escapes if you use single quotes for the outer string
I just use json.loads
@stray path this is the best I could think of
your solution is pretty close to the iterative case, but it's missing a loop over result_dicts
I did iterative approach
every time you have an array of values, you need to duplicate the elements in result_dicts
whats wrong with the code?
50%
ima let connor handle this actually
@deft geyser this is the output
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
make sure you're able to write it without looking at this example! i didn't mean to solve the problem haha
yes you can check out my github https://github.com/connorskees/
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.