Reputation: 33
My text file is laid out like this: 1st name, last name, wins, losses
jack,hofferon,5,6
zack,macker,0,11
alex,hof,11,0
dex,gerred,2,9
cassy,comp,1,10
import csv
cho_two=int(input('gg:'))
class_a = open("prac2.txt")
csv_a = csv.reader(class_a)
a_list = []
for row in csv_a:
row[0] = int(row[2])
row[1] = int(row[2])
row[2] = int(row[2])
row[3] = int(row[2])
a_list.append(row[0:3])
if cho_two == 1:
numerical = [[x[0]] for x in a_list]
print("\nCLASS A\nEach students highest by numerical order \n")
for alpha_order in sorted(numerical, reverse=True):
#csv_a.append(alpha_order)
print(alpha_order)
class_a.close()
Im trying to order a leaderboard by how many wins a player has and filter out player that have not gotten a single win and I have gotten as far as ordering the wins but i have no clue how to append the player info to the correct win amount.
Upvotes: 1
Views: 67
Reputation: 335
If you have nothing agains a bit data science approach, I recommend pandas
import pandas as pd
leaderboard = pd.read_csv('leaders.txt', header=None)
#filter those who don't have any wins (0 in third column)
leaderboard = leaderboard[leaderboard[2] > 0]
#sort by third column
leaderboard.sort(2, ascending = False)
And if you want to save the csv, you simply do leaderboard.to_csv('ordered.txt', header=False)
Upvotes: 0
Reputation: 888
The follosing code uses python OrderedDict
import csv
from collections import OrderedDict
a_dict= {}
with open('prac2.txt', 'rb') as csvfile: # User with open so no need to worry about forget closing the file.
csv_data = csv.reader(csvfile)
for row in csv_data:
# dictionary key is first name, value is number of wins
# You can combine fn and sn together as key
a_dict[row[0]] = int(row[2])
print "Unsorted"
for k, v in a_dict.items():
print "%s: %s" % (k, v)
# Now sort by value in reverse order
d_sorted_by_value = OrderedDict(sorted(a_dict.items(), key=lambda x: x[1],reverse=True))
print "Sorted"
for k, v in d_sorted_by_value.items():
print "%s: %s" % (k, v)
Output:
Unsorted
dex: 2
cassy: 1
alex: 11
jack: 5
zack: 0
Sorted
alex: 11
jack: 5
dex: 2
cassy: 1
zack: 0
Upvotes: 0
Reputation: 78780
First, I would create a name -> wins, losses mapping by reading the data into a dictionary.
>>> with open('prac2.txt') as f:
... stats = {}
... for line in f:
... first_name, last_name, wins, losses = line.split(',')
... name = '{} {}'.format(first_name, last_name)
... stats[name] = {'wins': int(wins), 'losses': int(losses)}
...
>>> stats
{'zack macker': {'wins': 0, 'losses': 11}, 'jack hofferon': {'wins': 5, 'losses': 6}, 'dex gerred': {'wins': 2, 'losses': 9}, 'alex hof': {'wins': 11, 'losses': 0}, 'cassy comp': {'wins': 1, 'losses': 10}}
After that, iterate over the items of that dictionary sorted by wins and print out the related information if the player has at least one win. Here's how that could look like:
>>> sorted_items = sorted(stats.items(), key=lambda x: x[1]['wins'], reverse=True)
>>> for name, info in sorted_items:
... wins = info['wins']
... if wins > 0:
... print('{} {}'.format(name, wins))
...
alex hof 11
jack hofferon 5
dex gerred 2
cassy comp 1
Upvotes: 1