Hole | Wins | Losses | Score | +25 |
---|---|---|---|---|
1 | 34 | 46 | +10 | (-2) |
10 | 30 | 51 | +25 | (E) |
Avg | 32 | 49 | +45 |
Here is the code in Python to parse and print the statistics of the Kansas City Royals baseball team from 1990 to 2001:
import re
def parse_stat(line):
match = re.match(r"(\d+)\s+([A-Za-z]+)\s+(\d+)", line)
if match:
wins, losses, games_played = match.groups()
return {
'wins': int(wins),
'losses': int(losses),
'games_played': int(games_played)
}
else:
return None
stat_dict = {}
for line in data.split('\n'):
if line: # skip empty lines
stats = parse_stat(line)
if stats:
stat_dict[stats['wins']] = {
'wins': stats['wins'],
'losses': stats['losses'],
'games_played': stats['games_played']
}
for year, stats in stat_dict.items():
print(f"{year}: {stats['wins']} wins, {stats['losses']} losses, {stats['games_played']} games played")
This code uses regular expressions to parse the lines of data. It assumes that each line has the format "X wins, Y losses, Z games played", where X is the number of wins, Y is the number of losses, and Z is the number of games played.
The parse_stat
function takes a line of data as input and returns a dictionary containing the parsed statistics. The stat_dict
variable stores all the parsed statistics in a nested dictionary structure.
Finally, the code prints out the statistics for each year.
Note: This code assumes that the data is stored in a string variable called data
. You should replace this with your actual data source. Updated: June 4, 2025 at 4:56 AM