Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

python - How to calculate a football team's total points received in a given season?

I'm actually trying to understand if I'm following the right path in a calculation. So there's a team, say Resen FC, with results in a season of 34 weeks as follows;

"3,1,1,1,0,0,3,1,1,3,3,0,0,1,3,1,3,3,1,1,0,0,0,0,3,1,1,1,0,3,3,0,1,1"

These numbers aren't necessarily specified as points received by the team but as representations of;

wins=3, draws=1, and losses=0

If each win was worth 3 points, each draw is 1, and a loss gets nothing, my calculation of this team's overall season record, as of the end of week 34 would be;

results = [3,1,1,1,0,0,3,1,1,3,3,0,0,1,3,1,3,3,1,1,0,0,0,0,3,1,1,1,0,3,3,0,1,1]
print(sum(results))

which gives the output of 44 points.

The team was supposed to be relegated had it got less than 40 points at the end of 34 weeks.

So I have couple of questions at this point:

  1. Is there an alternative way for calculating the results without having to hand-write them in a list as I did above?

  2. (might be tied to the first question) What if the representation of wins, draws and losses remained the same, but the points received for each result had been different?

Example:

 "3,1,1,1,0,0,3,1,1,3,3,0,0,1,3,1,3,3,1,1,0,0,0,0,3,1,1,1,0,3,3,0,1,1"

Each win (3) is worth 5 points,

Each draw(1) is worth 2 points,

Each loss(0) is worth 1 point.

How could I have been able to calculate the team's end-of-season record? Could I have used a function which receives the results as input and do the job for any input matching the format?

Note: This is my first question ever. So I'd be appreciated if you could just consider that before canceling me out :)

Thanks y'all.

question from:https://stackoverflow.com/questions/65893748/how-to-calculate-a-football-teams-total-points-received-in-a-given-season

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can simply convert your comma-separated string to a list by calling split(","), which will split your string by commas.

mystring = "3,1,1,1,0,0,3,1,1,3,3,0,0,1,3,1,3,3,1,1,0,0,0,0,3,1,1,1,0,3,3,0,1,1"
mylist = mystring.split(",")
#['3', '1', '1', '1', '0', '0', '3', '1', '1', '3', '3', '0', '0', '1', '3', '1', '3', '3', '1', '1', '0', '0', '0', '0', '3', '1', '1', '1', '0', '3', '3', '0', '1', '1']

To change the points of wins, draws and losses, you might want to create a dict.

winpoints, drawpoints, losepoints = 5,2,0
scoredict = {3:winpoints, 1:drawpoints, 0:losepoints}

Then, you can generate a new list of points and compute the sum.

newsum = sum([scoredict[int(v)] for v in mylist])
#78

In a regular for loop it would look like this

newlist = []
for v in mylist:
    newlist.append(scoredict[int(v)])
newsum = sum(newlist)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...