Bipod
Bipod

Reputation: 69

Trouble translating input into a dictionary

I am having trouble converting input from a file into a dictionary structure.

The input from the file has the format: questionNo user_id response

questionNo is the number of the question for which there is a response to. user_id uniquely identifies the person who made the response. response is the answer the user entered in response to the question.

and in the file looks something like this:

1 67 1
1 109 1
1 23 2
1 24 1
1 67 3
1 23 5
2 23 3
3 22 4

What I am trying to do is translate this data into a structure like this:

{user_id:{ questionNo:response, questionNo:response,.......},user_id{...}...}

with each questionNo and user_id being unique

My problem is that I have been totally unsuccessful in implementing this. It is like I have hit a mental roadbloack. I am not asking any of you to give me a coded solution, just some hints or tips that would help me solve this.

Thanks.

Upvotes: 1

Views: 55

Answers (3)

Alex Koukoulas
Alex Koukoulas

Reputation: 996

+1 for Ricardo's answer it seems the best solution although I wanted to give you my solution as well which I think looks a bit simpler ( and probably less efficient)

#----- Open file -----
f = open("file.extension","r")

#---- initialize dicts ----
dictionary = {}

#---- read first line -----#
line = f.readline()

#---- while line is not empty ----#
while line != "":
    #----- split the line -----#
    splitLine = line.split()

    #----- Get the 3 strings your need -----#
    questionNo = splitLine[0]
    user_id    = splitLine[1]
    response   = splitLine[2]

    #------ Check if user_id is not registered -----#
    if user_id not in dictionary:

        #------- Create the new entry -----#
        dictionary[user_id] = {questionNo:response}

    else:
        #------- Add the next questionNo along with the response -----#         
        dictionary[user_id][questionNo] = response

    #----- read new line ------#
    line = f.readline()

Upvotes: 0

sabbahillel
sabbahillel

Reputation: 4425

To create a dictionary you would start

mydict = {}

When adding a response for a particular user, you would say

mydict[user_id] = newvalue

If user_id has not yet been put into the dictionary, you would test by

if user_id not in mydict:
  mydict[user_id] = empty value # in your example it would either be [] or {}
mydict[user_id] = newvalue # This gets done under any circumstances

If the values are themselves dictionaries keyed on the question #, you would add the newvalue in a similar way to make every question have only the latest answer. If the question/answer are elements of a list pair, you would append a new question/answer entry and replace the answer in a question that is already in the list.

I see that you have your question/answer pairs as being in a dictionary, but I included a list reference for completeness.

I have to go now but this should give you a start.

Upvotes: 0

Ricardo Cárdenes
Ricardo Cárdenes

Reputation: 9172

I'm assuming you have read your entries from the file into an entries list, to make things simpler. I'll use then defaultdict to save us some logic:

from collections import defaultdict

dct = defaultdict(dict)

for questionNo, user_id, response in entries:
    dct[user_id][questionNo] = response

Upvotes: 5

Related Questions