How to randomly pair data in text file - python

I have two files: file(student) where there are 20 students and file(lecturer) where there are 3 lecturers. I want to pair the students and lecturers randomly. For example:
lecturer(1) = student(2),student(3),student(19)
lecturer(3) = student(20),student(23)......
This is the code I have tried. It is not behaving in the manner I had hoped for:
import random
lecturer = open("lecturer.txt", "r")
students = open("students.txt", "r")
spliti = lecturer.read().split("\n")
splitis = students.read().split("\n")
stud = (random.choice(splitis))
for stud in splitis:
file = open(stud + "txt","w")
for i in range():
questinss = random.choice(spliti)
file.write(lecturer + "\n")
files = open(students + ",txt", "r")
file.close()
lecturer.close()
students.close()

Here are some codes that you can use. Hope it can give you some thoughts.
import random
# get the students
with open('student.txt','r') as f:
students = f.read().split()
# get the lectures
with open('lecture.txt','r') as f:
lectures = f.read().spilt()
# since you only have three different lectures, we can sequently
# collect them as 0,1,2
reflist = []
for student in students:
reflist.append( lectures[ random.randrange(3) ] )
# prepare for the print
lecture_student = []
for lecture in lectures:
count = 0
ls = []
for ndx in reflist:
if lecture == ndx:
ls.append(students[count])
count += 1
lecture_student.append(ls)
# now to file them
with open('pro_lecture_student.txt','wt') as f:
count = 0
for lecture in lectures:
f.write(lecture)
f.write(': ')
for student in lecture_student[count]:
f.write(student)
f.write('\n\n')
count += 1

Related

Python grouping and randomization

I just started playing around with python and I
wanted to create a little project. I have a dictionary
"Teams" with name as key and score as values
(1-5). I want to create a code that randomizes the 15
players in 3 teams of 5 based on their score which
needs to be the average of their scores. Example: a
team needs to include top players (score 5) and
also weaker players (score 1 to 3) in order to create
an equilibrium.
If more details are needed please let me know :)
Cheers
Something similar to this perhaps?
import csv
import os
import random
names = []
userhome = os.path.expanduser('~')
csvfile = userhome + r'/Desktop/uni_spread.csv'
with open(csvfile, 'r') as f:
reader = csv.reader(f)
data = list(reader)
bracket1 = []
bracket2 = []
lenx = len(data)
lenx = int(lenx/2)
reshuffle = True
shuffleCount = 0
while (reshuffle == True) and (shuffleCount <= 100):
random.shuffle(data)
bracket1 = data[0:lenx]
bracket2 = data[lenx:]
for i in range((lenx-1)):
uni1 = str(bracket1[i])
uni2 = str(bracket2[i])
uni1,team1 = uni1.split('#')
uni2,team2 = uni2.split('#')
if uni1 == uni2:
reshuffle = True
break
reshuffle = False
shuffleCount +=1
print('final shuffle')
print("""
""")
if len(bracket1) != len(bracket2):
print('uneven teams, walkover granted to: ')
print(bracket1[(len(bracket1)-1)])
print("""
team matchup
""")
for i in range(lenx):
uni1 = str(bracket1[i])
uni2 = str(bracket2[i])
uni1,team1 = uni1.split('#')
uni2,team2 = uni2.split('#')
print( uni1, '', team1 , ' VS ', uni2, '', team2)
print('shuffle count is ', shuffleCount)

Syntax error calculating the average of student marks while reading from a text file

f = open('studMarks.txt', 'r')
marks = 0
# Sort out names, split the words then sort which order
for line in f:
words = line.split()
fname = words[0]
lname = words[1]
print(f"{lname},{fname}")
f.close()
f = open('studMarks.txt', 'r')
sum = 0
count = 0
for line in f:
count += 1
sum += float(line.split()[2])
n = []
average = sum/count
print(f"{average}")
When using the for loop it seems to display a value of 64.3, which I believe is for the total of the whole student list and average for all marks.
I need to produce the an output which displays the student names and average on the same line. I can do for the names but I cannot do it for the average as I keep getting errors. I don't know what to input in.
Below is the full solution. The with open line is a context manager and ensures that the file will get closed as soon as you exit the block. You should get used to using this style as it's the safe way to do I/O. The rest is just bog standard Python.
marks=dict()
with open('studMarks.txt', 'r') as f:
for line in f:
words = line.split()
fname = words[0]
lname = words[1]
score = int(words[2])
key = f'{fname} {lname}'
count_key = f'{fname} {lname}_count'
latest_score = score + (marks.get(key)[0] if marks.get(key) else 0)
latest_count = 1 + (marks.get(key)[1] if marks.get(key) else 0)
marks[key] = (latest_score, latest_count )
for name, value in marks.items():
print(f'{name} : {value[0]/value[1]}')
This is an interesting problem.
From what I understand you have a text file that looks like this:
Johnny Ly 90 100 Adam Best 80 30 Tim Smith 10 20 in a file called studentMarks2.txt
and want output like this:
Johnny_Ly 95.0 Adam_Best 55.0 Tim_Smith 15.0
if that is true then it can be done using code like this without pandas or csv
though those would make this a lot easier.
fileContents = []
with open('studMarks2.txt','r') as f:
fileContents = f.read().split()
students = dict()
names = []
for content in fileContents:
if content.isnumeric():
studentKey = '_'.join(names)
currentScore = students.get(studentKey,[])
newScore = currentScore + [float(content)]
students.update({studentKey:newScore})
else:
if len(names) == 2:
names.clear()
names.append(content)
else:
names.append(content)
for student,scores in students.items():
avg = sum(scores)/len(scores)
print(student,avg,end=' ')
Broken down
This part reads the contents and splits on white space
fileContents = []
with open('studMarks2.txt','r') as f:
fileContents = f.read().split()
this part then iterates through the contents
storing the names as keys in a dictionary and putting the scores in a list
students = dict()
names = []
for content in fileContents:
if content.isnumeric():
studentKey = '_'.join(names)
currentScore = students.get(studentKey,[])
newScore = currentScore + [float(content)]
students.update({studentKey:newScore})
else:
if len(names) == 2:
names.clear()
names.append(content)
else:
names.append(content)
Lastly it iterates over the dictionary and output the avg on one line
for student,scores in students.items():
avg = sum(scores)/len(scores)
print(student,avg,end=' ')

transform for in loop to while loop

i have this assignment in a basic programming course where i need to transform this code using while loop instead of for loop, but i dont know how to doit
this is my code so far
def read_txt(file_txt):
file = open(file_txt, "r")
lines = file.readlines()
file.close()
return lines
file_txt = input("file: ")
lines = read_txt(file_txt)
for l in lines:
asd = l.split(",")
length = len(asd)
score = 0
for i in range(1, length):
score += int(asd[i])
average = score / (length-1)
print(asd[0], average)
file text is like this
edward,4,3,1,2
sara,5,4,1,0
def read_txt(file_txt):
file = open(file_txt, "r")
lines = file.readlines()
file.close()
return lines
file_txt = input("file: ")
lines = read_txt(file_txt)
lines.reverse()
while lines:
l = lines.pop()
asd = l.split(",")
length = len(asd)
score = 0
i = 1
while i < length:
score += int(asd[i])
i += 1
average = score / (length-1)
print(asd[0], average)
Now in this while loop, it will iterate through lines until lines is empty. it will pop out items one by one.
For loops are more suitable for iterating over lines in files than while loops. Few improvements here are, (1) use the builtin sum instead of manually adding up scores, and (2) don't read all lines in file at once if the files are too big.
file_txt = input("file: ")
with open(file_txt) as f:
while True:
line = f.readline()
if not line:
break
name, scores = line.split(',', maxsplit=1)
scores = scores.split(',')
avg = sum(int(s) for s in scores) / len(scores)
print(f'{name} {avg}')
As you see above the check for if not line to determine if we have reached the end of file in a while loop, this is not needed in for loop as that implements the __iter__ protocol.
Python 3.8 walrus operator makes that slightly easier with::
file_txt = input("file: ")
with open(file_txt) as f:
while line := f.readline():
name, scores = line.split(',', maxsplit=1)
scores = scores.split(',')
avg = sum(int(s) for s in scores) / len(scores)
print(f'{name} {avg}')
The following gives the exact same output without using any for loop.
filename = input("file: ")
with open(filename) as f:
f = f.readlines()
n = []
while f:
v = f.pop()
if v[-1] == '\n':
n.append(v.strip('\n'))
else:
n.append(v)
d = {}
while n:
v = n.pop()
v = v.split(',')
d[v[0]] = v[1:]
d_k = list(d.keys())
d_k.sort(reverse=True)
while d_k:
v = d_k.pop()
p = d[v]
n = []
while p:
a = p.pop()
a = int(a)
n.append(a)
print(str(v), str(sum(n)/len(n)))
Output:
edward 2.5
sara 2.5

How to calculate moving average for temperature?

This is the output I need:
Temperature anomaly filename:SacramentoTemps.csv
Enter window size:60
1940,-0.2331
1941,-0.2169
1942,-0.2150
1943,-0.2228
1944,-0.2107
1945,-0.1796
1946,-0.1667
1947,-0.1582
1948,-0.1585
1949,-0.1492
1950,-0.1711
1951,-0.1688
1952,-0.1490
1953,-0.1556
1954,-0.1548
1955,-0.1580
1956,-0.1420
1957,-0.1101
1958,-0.1017
This is my code:
filename = input("Temperature anomaly filename:")
infile = open(filename, "r")
k = int(input("Enter window size:"))
infile.readline()
temp_list = []
for line in infile:
line = line.strip()
year,temp = line.split(",")
temp = float(temp)
temp_list.append(temp)
index = k
for index in range(index,len(temp_list)-1-index):
year = 1880 + index
ave = sum(temp_list[index:index+k]) / (2*index+1)
print(str(year)+","+"{:.4f}".format(ave))
infile.close()
My code currently prints out up until the year 1957 and it prints out the wrong averages for each year. What do I need to fix?
filename = "SacramentoTemps.csv"
infile = open(filename, "r")
k = int(input("Enter window size:"))
temp_list = []
for line in infile:
line = line.strip()
year, temp = line.split(",")
temp = float(temp)
temp_list.append(temp)
infile.close()
moving_average = []
for i, temp in enumerate(temp_list):
average = temp
if len(temp_list) - i < k:
break
for j in range(k):
average += temp_list[i+j]
moving_average.append(average/k)
print(str(year) + "," + "{:.4f}".format(average))
I coded in the direction of modifying your code as little as possible.
One thing to note is your file need to be longer than window size.
Using pandas would be most sane way to go:
import pandas as pd
filename = "SacramentoTemps.csv"
window = 2
data = pd.read_csv(filename)
data.temperature.rolling(window = window).mean().fillna(data.temperature)

Python: Average Prie per Year

Would anyone be able to help me with the below? I'm trying to create a program that can open the "notepad.txt" file and calculate the average price for the month of October.
notepad.txt
10-15-2012:3.886
10-22-2012:3.756
10-29-2012:3.638
infile = open('notepad.txt', 'r')
def clean_data():
line1 = infile.readline()
split1 = line1.rstrip('\n')
items = split1[0].split('-')
del items[0]
del items[0]
master = []
master = master + split1 + items
master = list(map(float, master))
print(master)
print(total)
line1 = infile.readline()
clean_data()
this prints and returns the average
def clean_data(infile):
lines = infile.readlines()
total = 0.0
num = 0
for line in lines:
spl = line.strip().split(":")
total += float(spl[len(spl)-1])
num += 1
average = total/num
print(average)
return average
def sum_data():
n,c = 0,0
with open('notepad.txt', 'r') as infile:
x = infile.readline()
# for october 10
if x[:3]=='10-' and x[6:10]=='2010';
n += float(x[12:])
c += 1
print(n/c)
If you want to use Pandas:
from io import StringIO
import pandas as pd
notepadtxt = StringIO("""10-15-2012:3.886
10-22-2012:3.756
10-29-2012:3.638""")
df = pd.read_csv(notepadtxt, sep='\:',header=None, engine='python')
df[0] = pd.to_datetime(df[0])
df=df.set_index(0)
df.resample('M').mean().values[0][0]
Output:
3.7600000000000002
The following vanilla Python code should suffice:
infile = open('notepad.txt', 'r')
def clean_data():
data = []
for line in infile:
data.append(line.strip().split(':'))
values = []
for value in data:
values.append(float(value[1]))
avg_price = sum(values)/len(values)
print(avg_price)
clean_data()
infile.close()

Categories