s.matthew.english s.matthew.english - 4 months ago 11
Python Question

adding simple mathematical operation to python script

I have a program that operates on a csv file to create output that looks like this:

724, 2
724, 1
725, 3
725, 3
726, 1
726, 0


I would like to modify the script with some simple math operations such that it would render the output:

724, 1.5
725, 3
726, 0.5


The script I'm currently using is here:

lines=open("1.txt",'r').read().splitlines()
for l in lines:
data = l.split('"Overall evaluation:')
if len(data) == 2:
print(data[0] + ", " + data[1])


How could I add a simple averaging and slicing operation to that pipeline?

I guess I need to create some temporary variable, but it should be outside the loop that iterates over lines?

Maybe something like this:

lines=open("EasyChairData.csv",'r').read().splitlines()

for l in lines:
data = l.split('"Overall evaluation:')

submission_number_repo = data[0]

if len(data) == 2:
print(data[0] + ", " + data[1])

if submission_number_repo != data[0]
submission_number_repo = data[0]





EDIT

The function is just a simple average

Answer

You can use a dictionary that map the key to the total and count and then print it:

map = {}
lines=open("1.txt",'r').read().splitlines()
for l in lines:
    data = l.split('"Overall evaluation:')
    if len(data) == 2:
        if data[0] not in map.keys():
            map[data[0]] = (0,0)
        map[data[0]] = (map[data[0]][0]+int(data[1]) , map[data[0]][1]+1)
for x, y in map.items():
    print(str(x) + ", " + str(y[0]/y[1]))