Skip to main content
added 105 characters in body
Source Link
Virge Assault
  • 1.4k
  • 6
  • 20
  • 43

I'm writing a python script that reads a players name and stats from a sentence in a .txt file, then updates their stats within a dictionary and then prints out their average stats. I'm having trouble with assigning multiple values to the same 'player' key, as well as getting the logic below it to correctly update the player stats. The .group part is giving me trouble too. How can I do this?

  import re, sys, os, math
 
 
if len(sys.argv) < 2:
    sys.exit("Usage: %s filename" % sys.argv[0])
 
filename = sys.argv[1]
 
if not os.path.exists(filename):
    sys.exit("Error: File '%s' not found" % sys.argv[1])

line_regex = re.compile(r"^(\w+ \w+) batted (\d+) times with (\d+) hits and (\d+) runs")
line = [line.strip() for line in open(filename)]

f = open (filename)

playerStats = {'players': [atBats[0, hits0, avgs]0]} 

for players in playerStats:
    player = line.group(1)
    atBat = line.group(2)
    hit = line.group(3)
    
    if player in playerStats:
        playerStats[player][1]    playerStats[player][0] += atBat
        playerStats[player][2]    playerStats[player][1] += hit

    if player not in players:
        player = line.group(1)
    playerStats[player][1]    playerStats[player][0] = line.group(2)atBat
    playerStats[player][2]    playerStats[player][1] = line.group(3)hit
        avgs = 0

    else: 
        playerStats[player][0] = player
    playerStats[player][1]    playerStats[player][0] = atBat
    playerStats[player][2]    playerStats[player][1] = hit
    playerStats[player][3]    playerStats[player][2] = 0

for player in players:
    avgs[player] = round(float(hits[player])/float(atBats[player]), 3) 

print "%s: %.3f" % (player, avgs[player])

Traceback (most recent call last): File "ba.py", line 19, in player = line.group(1) AttributeError: 'list' object has no attribute 'group'

I'm writing a python script that reads a players name and stats from a sentence in a .txt file, then updates their stats within a dictionary and then prints out their average stats. I'm having trouble with assigning multiple values to the same 'player' key, as well as getting the logic below it to correctly update the player stats. The .group part is giving me trouble too. How can I do this?

import re, sys, os, math
 

if len(sys.argv) < 2:
    sys.exit("Usage: %s filename" % sys.argv[0])
 
filename = sys.argv[1]
 
if not os.path.exists(filename):
    sys.exit("Error: File '%s' not found" % sys.argv[1])

line_regex = re.compile(r"^(\w+ \w+) batted (\d+) times with (\d+) hits and (\d+) runs")

f = open (filename)

playerStats = {'players': [atBats, hits, avgs]} 

for players in playerStats:
    player = line.group(1)
    atBat = line.group(2)
    hit = line.group(3)

if player in playerStats:
        playerStats[player][1] += atBat
        playerStats[player][2] += hit

if player not in players:
    player = line.group(1)
    playerStats[player][1] = line.group(2)
    playerStats[player][2] = line.group(3)
    avgs = 0

else: 
    playerStats[player][0] = player
    playerStats[player][1] = atBat
    playerStats[player][2] = hit
    playerStats[player][3] = 0

for player in players:
    avgs[player] = round(float(hits[player])/float(atBats[player]), 3) 

print "%s: %.3f" % (player, avgs[player])

I'm writing a python script that reads a players name and stats from a sentence in a .txt file, then updates their stats within a dictionary and then prints out their average stats. I'm having trouble with assigning multiple values to the same 'player' key, as well as getting the logic below it to correctly update the player stats. The .group part is giving me trouble too. How can I do this?

  import re, sys, os, math
 
if len(sys.argv) < 2:
    sys.exit("Usage: %s filename" % sys.argv[0])
 
filename = sys.argv[1]
 
if not os.path.exists(filename):
    sys.exit("Error: File '%s' not found" % sys.argv[1])

line_regex = re.compile(r"^(\w+ \w+) batted (\d+) times with (\d+) hits and (\d+) runs")
line = [line.strip() for line in open(filename)]

f = open (filename)

playerStats = {'players': [0, 0, 0]} 

for players in playerStats:
    player = line.group(1)
    atBat = line.group(2)
    hit = line.group(3)
    
    if player in playerStats:
            playerStats[player][0] += atBat
            playerStats[player][1] += hit

    if player not in players:
        player = line.group(1)
        playerStats[player][0] = atBat
        playerStats[player][1] = hit
        avgs = 0

    else: 
        playerStats[player][0] = player
        playerStats[player][0] = atBat
        playerStats[player][1] = hit
        playerStats[player][2] = 0

for player in players:
    avgs[player] = round(float(hits[player])/float(atBats[player]), 3) 

print "%s: %.3f" % (player, avgs[player])

Traceback (most recent call last): File "ba.py", line 19, in player = line.group(1) AttributeError: 'list' object has no attribute 'group'

added 118 characters in body
Source Link
Virge Assault
  • 1.4k
  • 6
  • 20
  • 43

I'm writing a python script that reads a players name and stats from a sentence in a .txt file, then updates their stats within a dictionary and then prints out their average stats. I'm having trouble with assigning multiple values to the same 'player' key, as well as getting the logic below it to correctly update the player stats. The .group part is giving me trouble too. How can I do this?

import re, sys, os, math
  

if len(sys.argv) < 2:
    sys.exit("Usage: %s filename" % sys.argv[0])
 
filename = sys.argv[1]
 
if not os.path.exists(filename):
    sys.exit("Error: File '%s' not found" % sys.argv[1])

line_regex = re.compile(r"^(\w+ \w+) batted (\d+) times with (\d+) hits and (\d+) runs")

f = open (filename)

playerStats = {'players': hits[atBats, atBatshits, avgsavgs]} 

for players in playerStats:
    ifplayer = line.group(1) in playerStats:
      atBat = line.group(2)
    atBatshit +== line.group(23)
 
if player in playerStats:
        hitsplayerStats[player][1] += line.group(3)
atBat
        playerStats[player][2] += hit

if line.group(1)player not in players:
        player = line.group(1)
        atBatsplayerStats[player][1] = line.group(2)
        hitsplayerStats[player][2] = line.group(3)
        avgs = 0

    else: 
        playersplayerStats[player][0] = player
        atBatsplayerStats[player][1] = atBat
        hitsplayerStats[player][2] = hit
        avgplayerStats[player][3] = 0

for player in players:
    avgs[player] = round(float(hits[player])/float(atBats[player]), 3) 

print "%s: %.3f" % (player, avgs[player])

I'm writing a python script that reads a players name and stats from a sentence in a .txt file, then updates their stats within a dictionary and then prints out their average stats. I'm having trouble with assigning multiple values to the same 'player' key, as well as getting the logic below it to correctly update the player stats. The .group part is giving me trouble too. How can I do this?

import re, sys, os, math
 
if len(sys.argv) < 2:
    sys.exit("Usage: %s filename" % sys.argv[0])
 
filename = sys.argv[1]
 
if not os.path.exists(filename):
    sys.exit("Error: File '%s' not found" % sys.argv[1])

line_regex = re.compile(r"^(\w+ \w+) batted (\d+) times with (\d+) hits and (\d+) runs")

f = open (filename)

playerStats = {'players': hits, atBats, avgs} 

for players in playerStats:
    if line.group(1) in playerStats:
            atBats += line.group(2)
            hits += line.group(3)

    if line.group(1) not in players:
        player = line.group(1)
        atBats = line.group(2)
        hits = line.group(3)
        avgs = 0

    else: 
        players = player
        atBats = atBat
        hits = hit
        avg = 0

for player in players:
    avgs[player] = round(float(hits[player])/float(atBats[player]), 3) 

print "%s: %.3f" % (player, avgs[player])

I'm writing a python script that reads a players name and stats from a sentence in a .txt file, then updates their stats within a dictionary and then prints out their average stats. I'm having trouble with assigning multiple values to the same 'player' key, as well as getting the logic below it to correctly update the player stats. The .group part is giving me trouble too. How can I do this?

import re, sys, os, math
 

if len(sys.argv) < 2:
    sys.exit("Usage: %s filename" % sys.argv[0])
 
filename = sys.argv[1]
 
if not os.path.exists(filename):
    sys.exit("Error: File '%s' not found" % sys.argv[1])

line_regex = re.compile(r"^(\w+ \w+) batted (\d+) times with (\d+) hits and (\d+) runs")

f = open (filename)

playerStats = {'players': [atBats, hits, avgs]} 

for players in playerStats:
    player = line.group(1)
    atBat = line.group(2)
    hit = line.group(3)

if player in playerStats:
        playerStats[player][1] += atBat
        playerStats[player][2] += hit

if player not in players:
    player = line.group(1)
    playerStats[player][1] = line.group(2)
    playerStats[player][2] = line.group(3)
    avgs = 0

else: 
    playerStats[player][0] = player
    playerStats[player][1] = atBat
    playerStats[player][2] = hit
    playerStats[player][3] = 0

for player in players:
    avgs[player] = round(float(hits[player])/float(atBats[player]), 3) 

print "%s: %.3f" % (player, avgs[player])
Source Link
Virge Assault
  • 1.4k
  • 6
  • 20
  • 43

Adding and accessing values to key in python dictionary

I'm writing a python script that reads a players name and stats from a sentence in a .txt file, then updates their stats within a dictionary and then prints out their average stats. I'm having trouble with assigning multiple values to the same 'player' key, as well as getting the logic below it to correctly update the player stats. The .group part is giving me trouble too. How can I do this?

import re, sys, os, math
 
if len(sys.argv) < 2:
    sys.exit("Usage: %s filename" % sys.argv[0])
 
filename = sys.argv[1]
 
if not os.path.exists(filename):
    sys.exit("Error: File '%s' not found" % sys.argv[1])

line_regex = re.compile(r"^(\w+ \w+) batted (\d+) times with (\d+) hits and (\d+) runs")

f = open (filename)

playerStats = {'players': hits, atBats, avgs} 

for players in playerStats:
    if line.group(1) in playerStats:
            atBats += line.group(2)
            hits += line.group(3)

    if line.group(1) not in players:
        player = line.group(1)
        atBats = line.group(2)
        hits = line.group(3)
        avgs = 0

    else: 
        players = player
        atBats = atBat
        hits = hit
        avg = 0

for player in players:
    avgs[player] = round(float(hits[player])/float(atBats[player]), 3) 

print "%s: %.3f" % (player, avgs[player])