I have a string that looks like the following
s = """nate : 9.5 ,8 ,5 ,8.7
moe : 7 ,6.8 , - ,1
sam : 6 ,4 , ,7.6
cat : 8 ,8.6 ,6 ,2"""
for the dash (- ) it should just be removed completely for ex: 6.8, - , 1 should become 6.8,1
for the blank parts, it should have 0 instead for ex 4, , 7.6 should become 4, 0 , 7.6
after that is all done I want to calculate the average of each person and display it as a dictionary
this what I have come up with and I couldn't manage to go any further
def avg(s):
s = s.replace(" - ,", ""). replace(", ,", ", 0 ,")
s= s.strip().splitlines()
students={}
for i in s:
s = i.strip().split(":")
students[s[0]]= s[1].strip().split(",")
Loop through the scores, skipping -, replacing empty strings with 0, and converting the rest to floats. Then calculate the average by dividing by the length, and store that in the students dictionary.
It's easier to replace individual list elements than replacing in the original lines, since your spacing is inconsistent.
def avg(s):
s= s.strip().splitlines()
students={}
for i in s:
s = i.split(":")
student = s[0].strip()
scores = [x.strip() for x in s[1].split(",")]
scores = [float(score) if score else 0 for score in scores if score != "-"]
students[student] = sum(scores) / len(scores) if scores else 0
# split s into lines and iterate over each line
for line in s.splitlines():
# init num_scores and total to zero for this student
num_scores = 0
total = 0.0
# get the name and the scores
name, scores = line.split(" : ")
# split scores on commas and iterate over each score
for score in scores.split(","):
# remove whitespace
score = score.strip()
# if we have something and it's not a dash
if score and score != "-":
num_scores += 1
total += float(score)
# print this student's name and average
print(f"{name}: average score {total/num_scores}")
Related
i am very new to this.i am trying to make this assignment for the last 6 hours(:_(),this is the 7th version of code im trying,i have a input file with the names and grades(10 names of students along with 3 grades per student,i try to input it but i can not see any results until an error pops up, i seem to be struggling with assigning the grades and getting a mean from it. is there any other way i can do this??
i have tried to split the input into seperate students then try to make the program read the name and grade and at last an average
with open('grades1.txt') as f:
content = [line.split() for line in f]
keys = content[0]
lst = list(zip([keys]*(len(content)-1), content[1:]))
x = [zip(i[0], i[1]) for i in lst]
z = [dict(i) for i in x]
Name,grade1,grade2,grade3 = x.split()
subjects = [float(name), float(grade1), float(grade2),
float(grade3)]
sum = 0
zero_count = 0
for subject in subjects:
sum += subject
if subject is 0:
zero_count +=
print(i, sum/(len(subjects)-zero_count)
nothing i try seems to work,
i keep getting syntax errors etc. here is link to the assignment:
https://i.stack.imgur.com/cu0q1.png\
input:
Tom______ 5 4 4.5
Dain ________6 7 7
Thorin ____8 8 3
Meriadoc ____1.0 2.3 4.5
Sam_________2.4 6.5 4.7
Gollem________________1.8 6.7 5.3
Frodo ________9.1 3.7 8.5
Gandalfe_____5.1 5.5 6.9
Peregrijn________3.0 8.5 3.1
Bruine____2.0 6.0 2.5
output:
Tom has an average grade of
Dain has an average grade of x
Thorinhas an average grade of x
Meriadoc has an average grade ofx x
Sam has an average grade of x
Gollem has an average grade of x
Frod has an average grade of x
Gandalf has an average grade of x
Peregrijn has an average grade of x
Bruine has an average grade of x
End of report
Not sure I understand your code but here's a fairly simple way to do it. See comments for explanation:
with open('grades1.txt') as f:
for line in f:
# Split on underscore to get name and grades (filtering out empty strings)
# Grades will be in one string, ex: "1.0 5.0 7.7"
name, grades_string = filter(None, line.split('_'))
# Split grades string on space and convert to each float
grades = [float(x) for x in grades_string.split(' ')]
# Calc average
average = sum(grades) / len(grades)
# Print results (TODO: formatting/rounding)
print name + ' has an average grade of ' + str(average)
I have a dataframe with score points 0 and 1 and corresponding reviews, I want to find the most common words in reviews with 0 points and 1 points. I tried this but it gives the count of all words:
count = defaultdict(int)
l = df['Summary']
for number in l:
count[number] += 1
print(count)
How can I find the most common values from all the rows with 1 score and 0 score?
Try using a frequency dict. If your columns can be viewed as a list of lists:
data = [[0, "text samle 1"], [0, "text sample 2"], [1, "text sample 3"]]
...then you can:
fd0 = dict()
fd1 = dict()
for list_item in data:
associated_value = list_item[0]
#note the split(' ') splits the string into a list of words
for word in list_item[1].split(' '):
if associated_value == 0:
fd0[word] = 1 if word not in fd0 else fd0[word] + 1
elif associated_value == 1:
fd1[word] = 1 if word not in fd1 else fd1[word] + 1
At the end of the loop your fd0 should have frequency for label 0 and fd1 should have frequency for label 1.
Assuming your data looks like this
review score
0 bad review 0
1 good review 1
2 very bad review 0
3 movie was good 1
You could do something like
words = pd.concat([pd.Series(row['score'], row['review'].split(' '))
for _, row in df.iterrows()]).reset_index()
words.columns = ['word', 'score']
print(words.groupby(['score', 'word']).size())
which gives you
score word
0 bad 2
review 2
very 1
1 good 2
movie 1
review 1
was 1
dtype: int64
most_common_0 = ''
most_common_1 = ''
for text, score in zip(df['Summary'], df['Score']):
if score == 1:
most_common_1 += ' ' + text
else:
most_common_0 += ' ' + text
from collections import Counter
c = Counter(most_common_1.split())
print(c.most_common(2)) # change this 2 to the number you want to analyze
Output
[('good', 2), ('and', 1)]
Score SynsetTerms
1 soft#18 mild#3 balmy#2
1 love#2 enjoy#3
0.625 love#1
From the input above, how can i achieve the output as below? I wish to create a file that has the word and their score by removing duplicate words, and splitting each word into a new row. Duplicated words with higher score will be selected instead.
Score SynsetTerms
1 soft
1 mild
1 balmy
1 enjoy
1 love
note that the word 'love' with 0.625 score was removed, only the 'love' with score 1 is kept as it has higher score.
import re
lst = []
dict = {}
i = 1
fhand = open('C:/Users/10648879/Documents/python_prog/data/test.csv', 'r')
for line in fhand:
if i == 1:
i = i + 1
continue
line = re.sub('#[0-9]*', '', line).strip()
line = re.split('\s+', line)
for counter in range(len(line)):
if counter == 0:
score = line[counter]
continue
if line[counter] in dict:
if score > dict[line[counter]]:
dict[line[counter]] = score
else :
dict[line[counter]] = score
i = i + 1
print 'score' + ' ' + 'SynsetTerms'
for k, v in dict.items():
print v, k
i have to create a lucky name number programme in python but i keep coming across many errors.
if you don't know what a lucky name number is, it is basically where each letter of the alphabet has a value and you add the values toether in your first name and second name so for example
john doe
165 465
1+6+5 = 12
4+6+5 = 15
15 + 12 = 27
2+7 = 8
then 8 = has diplomatic skills
Here is what i have done so far:
#this will go all the way to z
charDict = { 'A' : 1, 'B' : 2, 'C' : 3, 'D' : 4}
# example names - while loop will go here
firstName = 'AAB'
lastName = 'DCDD'
# split the strings into a list of chars
firstNameChars = list(firstName)
lastNameChars = list(lastName)
# sum up values
firstNameSum = 0
lastNameSum = 0
for chr in firstNameChars:
firstNameSum += charDict[chr]
for chr in lastNameChars:
lastNameSum += charDict[chr]
# cast sums to strings. In this example, this would be '2024'
combinedNames = str(firstNameSum) + str(lastNameSum)
# split the string into a list of chars
combinedNameDigits = list(combinedNames)
# sum them up
finalSum = 0
for dgt in combinedNames:
finalSum += int(dgt)
# print the lucky number
print finalSum
So my question is, is where do i go from here, as the numbers don't add up correctly and the values of the letters aren't correct, so basically how do i do the calculations correctly
I really don't undersand how: john doe gives 165 465 and how: AAB DCDD gives 2024.
However, the standard way to convert letters in numbers and to sum the digits of a number is the following:
def letters_to_numbers(name):
sum_ = 0
for letter in name:
sum_ += ord(letter.upper())-64 #ord("A")=65 minus 64 -> 1
return sum_
def sum_digits(number):
sum_ = 0
while number:
sum_ += number%10
number //=10
return sum_
sum_digits(
sum_digits(letters_to_numbers("john"))
+sum_digits(letters_to_numbers("doe")))
This works considering you always split your numbers to the digit level, I let you wrap it in a function and modify the dictionary to add other letters
first = 'aaa'
last = 'bbb'
name = first + last
dicti = {'a':1, 'b':2, '1':1, '2':2 , '3':3 , '4':4 , '5':5 , '6':6 ,
'7':7 , '8':8 , '9':9 , '0':0}
while len(name) >1:
sum = 0
for letter in name:
sum = sum + dicti[letter]
name = str(sum)
final_sum = int(name)
I have to create a program that shows the arithmetic mean of a list of variables. There are supposed to be 50 grades.
I'm pretty much stuck. Right now I´ve only got:
for c in range (0,50):
grade = ("What is the grade?")
Also, how could I print the count of grades that are below 50?
Any help is appreciated.
If you don't mind using numpy this is ridiculously easy:
import numpy as np
print np.mean(grades)
Or if you'd rather not import anything,
print float(sum(grades))/len(grades)
To get the number of grades below 50, assuming you have them all in a list, you could do:
grades2 = [x for x in grades if x < 50]
print len(grades2)
Assuming you have a list with all the grades.
avg = sum(gradeList)/len(gradeList)
This is actually faster than numpy.mean().
To find the number of grades less than 50 you can put it in a loop with a conditional statement.
numPoorGrades = 0
for g in grades:
if g < 50:
numPoorGrades += 1
You could also write this a little more compactly using a list comprehension.
numPoorGrades = len([g for g in grades if g < 50])
First of all, assuming grades is a list containing the grades, you would want to iterate over the grades list, and not iterate over range(0,50).
Second, in every iteration you can use a variable to count how many grades you have seen so far, and another variable that sums all the grades so far. Something like that:
num_grades = 0
sum_grades = 0
for grade in grades:
num_grades += 1 # this is the same as writing num_grades = num_grades + 1
sum_grades += sum # same as writing sum_grades = sum_grades + sum
Now all you need to do is to divide sum_grades by num_grades to get the result.
average = float(sum_grade)s / max(num_grades,1)
I used the max function that returns the maximum number between num_grades and 1 - in case the list of grades is empty, num_grades will be 0 and division by 0 is undefined.
I used float to get a fraction.
To count the number of grades lower than 50, you can add another variable num_failed and initialize him to 0 just like num_counts, add an if that check if grade is lower than 50 and if so increase num_failed by 1.
Try the following. Function isNumber tries to convert the input, which is read as a string, to a float, which I believe convers the integer range too and is the floating-point type in Python 3, which is the version I'm using. The try...except block is similar in a way to the try...catch statement found in other programming languages.
#Checks whether the value is a valid number:
def isNumber( value ):
try:
float( value )
return True
except:
return False
#Variables initialization:
numberOfGradesBelow50 = 0
sumOfAllGrades = 0
#Input:
for c in range( 0, 5 ):
currentGradeAsString = input( "What is the grade? " )
while not isNumber( currentGradeAsString ):
currentGradeAsString = input( "Invalid value. What is the grade? " )
currentGradeAsFloat = float( currentGradeAsString )
sumOfAllGrades += currentGradeAsFloat
if currentGradeAsFloat < 50.0:
numberOfGradesBelow50 += 1
#Displays results:
print( "The average is " + str( sumOfAllGrades / 5 ) + "." )
print( "You entered " + str( numberOfGradesBelow50 ) + " grades below 50." )