Answer the question
In order to leave comments, you need to log in
How can sys.stdin which looks like a dictionary be turned into a dictionary?
sys.stdin looks like this:
{'Action': ['Terminal Velocity;1994', 'Terminator 2: Judgment Day;1991', 'Terminator, The;1984', 'Terminator 3: Rise of the Machines;2003', ' Terminator Salvation;2009', 'Terminator Genisys;2015'], 'Mystery': ['Terminal Velocity;1994'], 'Thriller': ['Terminal Velocity;1994', 'Terminator, The;1984', 'Terminator Salvation ;2009', 'Terminator Genisys;2015'], 'Sci-Fi': ['Terminator 2: Judgment Day;1991', 'Terminator, The;1984', 'Terminator 3: Rise of the Machines;2003', ' Terminator Salvation;2009', 'Terminator Genisys;2015'], 'Adventure': ['Terminator 3: Rise of the Machines;2003', 'Terminator Salvation;2009', 'Terminator Genisys;2015'], 'Comedy': ['Terminal, The;2004'], 'Drama': ['Terminal, The;2004'], 'Romance': ['Terminal, The;2004']}
me need to continue to use this dictionary
How can I turn it into a dict variable?
here is my code
# sort.py
import sys
import json
def dict_sorted_by_genre(dict_data):
# sort genres by alphabetically
sorted_result = sorted(dict_data.items(), key=lambda x: x[0])
dict_result = dict(sorted_result)
return dict_result
if __name__ == "__main__":
dict = json.loads(str(sys.stdin))
sorted_dict = dict_sorted_by_genre(dict)
print(sorted_dict)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question