Firstly, this is homework for my computing science class, but we are allowed to get help on the internet or using any other way we want.

I'm trying to write a program to read in a file with user responses to questions (I.E. an automated handset vote) and then to count the number of votes for each answer for each question. For some questions a handset may vote more than once (change their answer) and only the last vote should be counted.

At the moment, i've managed to read in the file, and store it as a dictionary, but i've got no idea how to count the number of votes for each answer?

The file Responses.txt contains the following data in the following format:
Question_number User_ID Answer

1 23 1
1 18 1
1 28 2
1 24 1
1 12 3
1 26 3
1 29 1
1 23 5
2 23 3
3 17 1

from string import *

responsesfile = open("Responses.txt","r")

def getResponses(responsesfile):
    responses = {}
    for line in responsesfile:
        line = line.strip()
        parts = [p.strip() for p in line.split(' ')]
        responses[parts[1]] = (parts[0], parts[2])
    return responses

responses = getResponses(responsesfile)

Any help is much appreciated.

Recommended Answers

All 3 Replies

Why you have the import statement?

i've managed to read in the file, and store it as a dictionary,

Probably not in the way you think. Print the dictionary and see what it contains. The dictionary key is the respondant's number, not the question number so at best you can count how many questions the respondant voted on, which is not what the question asked. There aren't many responses to this thread because the standard way to do this is with groupby

from itertools import groupby

test_data="""1 23 1
1 18 1
1 28 2
1 24 1
1 12 3
1 26 3
1 29 1
1 23 5
2 23 3
3 15 2
3 17 1 """
things=test_data.split("\n")

for group in groupby(things, lambda x: x[0]):
    for ctr, gen in enumerate(group):
        print ctr, list(gen)

but you will probably say that you can't use, or don't know how to use groupby. This might happen for the next solution as well, etc, etc.. So it is difficult to come up with a solution for someone who doesn't have any "count" code (and didn't even test the dictionary contents by printing it), i.e. putting each record into a dictionary is no different from reading each record from a file when it comes to counting something. Come up with a better solution, at least on paper, and code what you can and post back in a new thread with a better attempt.

You can shorten this:

for line in responsesfile:
line = line.strip()
parts = [p.strip() for p in line.split(' ')]
responses[parts[1]] = (parts[0], parts[2])
return responses

By doing this:

responses={line.strip()[1] : (line.strip()[0], line.strip()[2]) for line in responses file}

Note this syntax is only valid for python 2.7 or better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.