Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 From string parameter to a dictionary
#1
Dear members, is there anybody would can help my with my homework question?
I'm able to open the csv file. But from there I really have no idea what to do.

The assignment:

Steven is working on his Master thesis and has some ratings combined in a file for his participants. Each line in this file contains a participant number, the lesson number and the rating separated by a semicolon (";") like:

participant_id;lesson_number;rating
103;4;2
103;5;1


He wants to run some analyses and wants to combine ratings of certain lessons in a dictionary. This dictionary should have the participant number as key and a list of ratings belonging to that participant as value. The index of the rating should be the same as the lesson, in other words, the first spot of the list is the rating of lesson 1, the second sport of the list is the rating of lesson 2 etc.
- You can assume that there are seven lessons in total.
- Participant number is an integer
- Rating is a float

Steven also has a lot of missing data, you should put an "NA" (not applicable) on each spot where he doesn't have a rating.

Write a function "ratings" which takes a string parameter "filename" as input. It must return a dictionary as described.

For example,
ratings("ratings.csv")returns
{'103': [2.0, 'NA', 'NA', 'NA', 'NA', 2.0 , 'NA']}

To test this function with this file, create a file named "ratings.csv" in the same directory as your solution and copy and paste the following text in the file:

participant_id;lesson_number;rating
103;1;2
103;6;2



Quote
#2
Quote:can help my with my homework question?
We are all glad to help with your homework assignment.
before this can happen:
  • Make an attempt at solving the problem
  • If you succeed, Great! If Not:
    • Post your code.
    • Point out where you think you are having a problem.

As stated, we are glad to help, but we will not do your homework for you.
Quote
#3
And when you post your code, please use Python tags, and please give the full text of any error messages you receive.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#4
def ratings(file):
    with open(file, "r") as dc:
        buffer = dc.readlines()
    
ratings("ratings.csv")
This is what i use for open the file, after i did this, my output is the following line:
['participant_id;lesson_number;rating 103;1;2 103;6;2']

I wonder how I can split the line in the format it should be: {'103': [2.0, 'NA', 'NA', 'NA', 'NA', 2.0 , 'NA']}.

Is there somebody who can help me with this case?
Quote
#5
I don't think that's what readlines is giving you. I expect it is giving you ['participant_id;lesson_number;rating', '103;1;2', '103;6;2']. This is a list of strings. This is what you need to do: initialize an empty dictionary. Loop through each string the realines list (except the first one). For each one, pull out the student id, lesson number, and rating; convert them to integers; if the student id isn't in the dictionary add it with seven NAs, then put the rating in for the correct lesson.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#6

def ratings(file):
    empty_dict= []
    with open(file, "r") as dc:
        buffer = dc.readlines()
        for l in buffer: 
                    empty_dict.append(l)
    return empty_dict
    
    
ratings("ratings.csv")

is this a good start?
Quote
#7
No, that's a list ([]) not a dictionary ({}). You also want to skip the first line, so for l in buffer[1:]:. You still need to break down the lines with the strip method, and convert the items in the lines with the int() built-in. Then you need to make sure the student is the dictionary, set up the NAs if they aren't, and add the rating for the correct class. Adding the rating will required you to index the dictionary and the list within the dictionary.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#8
First of all, i really i appreciate your help!
This step is where i get confused. I know they
.strip()
method, but don't know how to combine in within this case. Also i'm confused with the why how I can add something to the empty dictionary, is it with
.append
or do i need to use
 += empty_dict 
Quote
#9
Split is a method to a string and comes after it with the dot operator. You pass another string to it, and it splits the string into a list, dividing the base string everywhere the substring exists. So 'big,pointy,teeth'.split(',') returns [/inline]['big', 'pointy', 'teeth'][/inline].

To add to a new dictionary, you just assign to a new key:

>>> d = {}
>>> d[1] = 2
>>> d['a'] = 'b'
>>> d
{1: 2, 'a': 'b'}
>>> d[1] = 5
>>> d
{1: 5, 'a': 'b'}
Edit: Corrected strip/split mistake Larz noted.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#10
strip() removes white space and newline from beginning and end of string
confused with split()
ichabod801 likes this post
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)