in Education by
Just started working with Python and I would like to know how to create a set only from user input. I'm working on a project where a student has to take some courses, however there are some prerequisites to some of the courses. The first line of input from the user will be the number of courses and the total number of dependencies, as such: 5 4 The next input (however many there may) will be the course IDs and what prerequisites the courses may have, such as: 1 3 2 3 4 1 4 2 Currently I am just creating it myself, like this: data ={ '1': set('3'), '2': set('3'), '4': set('1'), '4': set('2') } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
What kind of input are you expecting? And does the value in your dictionary have to be a set? If it's a dictionary, the key 4 actually doesn't work as you intend it to: set('1') and set('2') would overwrite each other if you updated the key. Assuming it's from a file with that specific format and you use sets, you could do something like this: data = {} with open("file.txt", "r") as f: for line in f.readlines(): parsed_line = line.split() key, value = parsed_line[0], parsed_line[1] if key in data: data[key].add(value) else: data[key] = set(value) This would result in something like: {'1': {'3'}, '2': {'3'}, '4': {'1', '2'}}

Related questions

0 votes
    A _____________ takes executable file as input and tries to generate high level code. (a) Debugger (b) ... -for-Cyber Security:,Cyber Security-Jobs:,Cyber Security Applications...
asked Oct 31, 2021 in Education by JackTerrance
0 votes
    Windows will automatically generate a domain-wide __________ security certificate for any user who encrypts a file for the first time. Select the correct answer from above options...
asked Dec 29, 2021 in Education by JackTerrance
0 votes
    How does a user generate multiple keydown events? (a) Repeating the same process (b) Pressing multiple keys ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 22, 2021 in Education by JackTerrance
0 votes
    I have an XML file which is structured as follows: place1 location1 place2 location2 place3 location3 These ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    How can we take input text from user in HTML page? (a) input tag (b) inoutBufferedReader tag (c) meta ... & Servlet of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    So I am trying to build a range pinger which job will be to use 2 IP addresses and then compare ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    WAP in java to input 3 numbers from user and print the largest no. among them using nested if. Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    WAP in java to input 2 numbers from user and print their difference (Always Positive). Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    How can we take input text from user in HTML page? (a) input tag (b) inoutBufferedReader tag (c) ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    How can I ask the user to enter a value during the execution of a GnuPlot script? I would like ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    So the idea is that users can create collections with many pairs of words in it. For example, they ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    Helllo I am still new to programing and had a question about using if statements while using user input ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    are set to control the pro .. are used to input information in a more user friendly way than tables. fa database ... in a . table Select the correct answer from above options...
asked Nov 28, 2021 in Education by JackTerrance
...