Strings and Integers in Python - python

This is my data file (called “studentdata.txt”)
joe 10 15 20 30 40
bill 23 16 19 22
sue 8 22 17 14 32 17 24 21 2 9 11 17
grace 12 28 21 45 26 10
john 14 32 25 16 89
I need to calculate the average grade for each student and print out the student’s name along with their average grade. I can extract the name with no problem and determine the number of exam scores, but I can not figure out how to sum the exam scores. This is what I have so far:
file=open("studentdata.txt","r")
for aline in file:
data=aline.split()
print((data[0]),"Average grade:")
print(len(data[1:]))
file.close()

It seems like you have most of this already done, and you already have a good grasp of how to partition each line into the two components you need, so you're real close!
First, since the data is being read in as a string, you need to convert part of your data to integers:
for line in file:
tmp = line.split()
name, scores = tmp[0], list(map(int, tmp[1:]))
This will give us each name, along with a list of scores as integers. Now all you have to do is find the average:
average = sum(scores)/len(scores)
Let's tie it all together by assigning to a dictionary:
dct[name] = average
And we get:
{'joe': 23.0, 'bill': 20.0, 'sue': 16.166666666666668, 'grace': 23.666666666666668, 'john': 35.2}

Try this?
file = open("studentdata.txt", "r")
for aline in file:
data = aline.split()
# Convert list of string numerals to int
grades = [int(grade) for grade in data[1:]]
# Find average by dividing sum by length of numbers list
average = sum(grades)/len(data[1:])
print((data[0]), "Average grade:", str(average))
file.close()

Try the below code, just split each line on spaces then get the numbers not with the name so the indexing will be the i.strip().split()[1:] then use map to convert that into an integer then use statistics.mean to get the average:
from statistics import mean
d = {}
with open('studentdata.txt','r') as f:
for i in f.readlines():
d[i.split()[0]] = mean(list(map(int,i.strip().split()[1:])))
print(d)
Output:
{'joe': 23.0, 'bill': 20.0, 'sue': 16.166666666666668, 'grace': 23.666666666666668, 'john': 35.2}

Related

How can I put text efficient in sub lists inside a list? (Python)

I wrote some code to calculate the maximum path sum of a triangle. This is the triangle:
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65
So the maximum path sum of this triangle is: 75+95+82+87+82 = 418
This is my code to calculate it:
lst = [[72],
[95,64],
[17,47,82],
[18,35,87,10],
[20,4,82,47,65]]
something = 1
i = 0
mid = 0
while something != 0:
for x in lst:
new = max(lst[i])
print(new)
i += 1
mid += new
something = 0
print(mid)
As you can see I put every item of the triangle down in lists and put the lists in a (head) list. This are not a lot numbers, but what if I have a bigger triangle? To do it manually is a lot of work. So my question is: How can I put the numbers from the triangle efficient in sub lists inside a head list?
If you have input starting with a line containing the number of rows in the triangle, followed by all the numbers on that many rows, read the first number to get the limit in a range(). Then use a list comprehension to create the list of sublists.
rows = int(input())
lst = [list(map(int, input().split())) for _ in range(rows)]
For instance, to read your sample triangle, the input would be:
5
75
95 64
17 47 82
18 35 87 10
20 04 82 47 65

how to remove a string from a list in Python

the first element of the list is a string and the rest are numbers. I want to perform mathematical operations between the numbers like median etc but I can't remove the first element a.k.a the string because I keep getting this error
AttributeError: 'str' object has no attribute 'pop'
I tried pop, remove etc
with open('/Users/solidaneziri/Downloads/Data_Exercise_1.txt') as infile:
for line in infile:
alter = line.split()[0]
alter.pop()
statistics.median(alter)
print(Alter)
Alter
28
25
28
26
22
20
25
21
21
25
24
25
26
22
26
20
27
22
22
26
23
20
22
26
24
22
20
20
19
21
19
19
33
23
21
29
21
25
26
19
23
20
25
21
input(alter)
output = 22.5 the median
Simply skip the first line. next consumes one element of the file iterator:
import statistics
with open('/Users/solidaneziri/Downloads/Data_Exercise_1.txt') as infile:
header = next(infile)
data = [int(line.split()[0]) for line in infile]
print(statistics.median(data))
Output:
22.5
Your mistake is taking the first element of the List at line.split()[0].
If each line is formed by a string followed by numbers, and you just want to remove the first string, it should be done like this:
with open('/Users/solidaneziri/Downloads/Data_Exercise_1.txt') as infile:
for line in infile:
alter = line.split()
alter.pop(0)
statistics.median(line)
Notice as well that you must introduce an index of the position of the List that you want to delete with pop().
Get rid of the string by not including the 0 index, then convert the strings representing numbers to actual floats so that they can be used in your calculations.
with open('/Users/solidaneziri/Downloads/Data_Exercise_1.txt') as file:
for line in file:
numbers = line.split()[1:]
numbers = map(float, numbers)
statistics.median(numbers)
You can use something like this:
import statistics
list_ = []
with open('1.txt') as infile:
for line in infile:
if line.split()[0].isdigit():
list_.append(int(line.split()[0]))
statistics.median(list_)
test_list = ["bad", 2, 4, 6 ,1, 3]
print ("Original list : " + str(test_list))
R = "bad"
while(R in test_list) :
test_list.remove(R)
print ("List after removing string " + str(test_list))
my_list = ["one", "two", "three", "four", "five", "six"]
print (my_list)
let's say i want the remove one and four
my_list.pop(3)
my_list.pop(0)
print(my_list)
#The output will be
["two", "three", "five", "six"]

Formatting Number Outputs using Fixed Point decimal precision while still removing trailing Zeros? (Python 3.7.7)

PYTHON 3.7.7
For a school assignment, I have to submit code using an evil program called Mimir. Basically if my output is not pixel-perfect, I get a fail on the test case (15min writing a code, 50min making the output "look right").
Question:
I have no clue how to get my number outputs to look like the assignment's. My shown attempt here uses the 'g' format, as it is the only format modifier that removes trailing zeros and removes the decimal point when necessary. I need to be able to do this while maintaining a '.6' precision. Also, how do I make it that the output is never displayed in scientific notation.
Thank you for the help!
MY CODE:
#Asking user to define parameters for the calculation
Organs = float(input('Starting number of organisms: \n'))
DailyInc = float(input('Average daily increase: \n'))
DaysX = int(input('Number of days to multiply: \n'))
#Doing % to decimal calc. only one to make program work faster
Inc = (1 + (DailyInc / 100))
#Print table heading and first day
print('Day Approximate Population')
print('1', ' ', format(Organs, 'g'))
#Loop to calculate running daily total and print using format hell
for Day in range (2, DaysX + 1):
Organs = Organs * Inc
if Day >= 100:
print(Day," ", format(Organs, 'g'))
elif Day >= 10:
print(Day," ", format(Organs, 'g'))
else:
print(Day," ", format(Organs, 'g'))
MY OUTPUT (input - 2, 30, 60):
Starting number of organisms:
Average daily increase:
Number of days to multiply:
Day Approximate Population
1 2
2 2.6
3 3.38
4 4.394
5 5.7122
6 7.42586
7 9.65362
8 12.5497
9 16.3146
10 21.209
11 27.5717
12 35.8432
13 46.5962
14 60.575
15 78.7475
16 102.372
17 133.083
18 173.008
19 224.911
20 292.384
21 380.099
22 494.129
EXPECTED OUTPUT (input - 2, 30, 60):
Starting number of organisms:
Average daily increase:
Number of days to multiply:
Day Approximate Population
1 2
2 2.6
3 3.38
4 4.394
5 5.7122
6 7.42586
7 9.653618
8 12.549703
9 16.314614
10 21.208999
11 27.571698
12 35.843208
13 46.59617
14 60.575021
15 78.747528
16 102.371786
17 133.083322
18 173.008318
19 224.910814
20 292.384058
21 380.099275
22 494.129058
try something like this:
val1 = 1.2635485547884475
val2 = 1.2
print(val1)
print(f'{val1:.2f}')
print(f'{val1:.6f}')
print(f'{val2:.6f}')
output:
1.2635485547884475
1.26
1.263549
1.200000
First example is displayed on a single line to demonstrate formating
val1 = 1.2635485547884475
val2 = 1.2
print(f'{val1:<17.15f}', end = " ")
print(f'{val1:<7.5f}', end = " ")
print(f'{val2:<4.2g}')
print(f'{val1:<17.15f}')
print(f'{val1:<7.5f}')
print(f'{val2:<4.2g}')

Reading txt file with number and suming them python

I have txt file witht the following txt in it:
2
4 8 15 16 23 42
1 3 5
6
66 77
77
888
888 77
34
23 234 234
1
32
3
23 23 23
365
22 12
I need a way to read the file and sum all the numbers.
i have this code for now but not sure what to do next. Thx in advance
`lstComplete = []
fichNbr = open("nombres.txt", "r")
lstComplete = fichNbr
somme = 0
for i in lstComplete:
i = i.split()`
Turn them into a list and sum them:
with open('nombres.txt', 'r') as f:
num_list = f.read().split()
print sum([int(n) for n in num_list])
Returns 3227
Open the file and use read() method to get the content and then convert string to int, use sum() to get the result:
>>> sum(map(int,open('nombres.txt').read().split()))
3227

find average value from CSV columns that contain a specific character

I am trying to get a simple python function which will read in a CSV file and find the average for come columns and rows.
The function will examine the first row and for each column whose header
starts with the letter 'Q' it will calculate the average of values in
that column and then print it to the screen. Then for each row of the
data it will calculate the students average for all items in columns
that start with 'Q'. It will calulate this average normally and also
with the lowest quiz dropped. It will print out two values per student.
the CSV file contains grades for students and looks like this:
hw1 hw2 Quiz3 hw4 Quiz2 Quiz1
john 87 98 76 67 90 56
marie 45 67 65 98 78 67
paul 54 64 93 28 83 98
fred 67 87 45 98 56 87
the code I have so far is this but I have no idea how to continue:
import csv
def practice():
newlist=[]
afile= input('enter file name')
a = open(afile, 'r')
reader = csv.reader(a, delimiter = ",")
for each in reader:
newlist.append(each)
y=sum(int(x[2] for x in reader))
print (y)
filtered = []
total = 0
for i in range (0,len(newlist)):
if 'Q' in [i][1]:
filtered.append(newlist[i])
return filtered
May I suggest the use of Pandas:
>>> import pandas as pd
>>> data = pd.read_csv('file.csv', sep=' *')
>>> q_columns = [name for name in data.columns if name.startswith('Q')]
>>> reduced_data = data[q_columns].copy()
>>> reduced_data.mean()
Quiz3 69.75
Quiz2 76.75
Quiz1 77.00
dtype: float64
>>> reduced_data.mean(axis=1)
john 74.000000
marie 70.000000
paul 91.333333
fred 62.666667
dtype: float64
>>> import numpy as np
>>> for index, column in reduced_data.idxmin(axis=1).iteritems():
... reduced_data.ix[index, column] = np.nan
>>> reduced_data.mean(axis=1)
john 83.0
marie 72.5
paul 95.5
fred 71.5
dtype: float64
You would have a nicer code if you change your .csv format. Then we can use DictReader easily.
grades.csv:
name,hw1,hw2,Quiz3,hw4,Quiz2,Quiz1
john,87,98,76,67,90,56
marie,45,67,65,98,78,67
paul,54,64,93,28,83,98
fred,67,87,45,98,56,87
Code:
import numpy as np
from collections import defaultdict
import csv
result = defaultdict( list )
with open('grades.csv', 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
for k in row:
if k.startswith('Q'):
result[ row['name'] ].append( int(row[k]) )
for name, lst in result.items():
print name, np.mean( sorted(lst)[1:] )
Output:
paul 95.5
john 83.0
marie 72.5
fred 71.5

Categories