我对堆栈溢出比较陌生,所以请多多包涵。另外,我为标题和任何歧义等道歉。
所以,这就是我想要做的:
我正在获取一个 .CSV 格式的原始数据文件,其中包含整个赛季 NBA 比赛的比赛。
该代码基于游戏创建一个新文件,该文件具有适当的游戏标题。然后,它读取每一行。
每行包含游戏 ID、行号(无关)、剩余时间和“游戏信息”。
我在下面包含了一个示例(示例 1)。
需要写入的信息如下所示(示例 2),球场上有 10 名球员:
- 录制该剧的时间
- 时期
- 期间剩余的时间
- 事件类型
- 如果有帮助
- 助攻的球员
- 如果有犯规
- 被犯规的人
等等
“玩家”标题指定了为谁记录了初始比赛(即,如果一个玩家进行了“击球”,则记录了该玩家的姓名)。录制该剧的球队也被写入。
示例 1:
GameID, LineNumber, TimeRemaining, Entry
20071030HOULAL, 1, 0:48:00, Start of 1st Quarter
20071030HOULAL, 2, 0:48:00, Jump Ball Brown vs Yao
20071030HOULAL, 3, 0:47:42, [LAL] Walton Jump Shot: Missed
20071030HOULAL, 4, 0:47:41, [LAL] Brown Rebound (Off:1 Def:0)
20071030HOULAL, 5, 0:47:29, [LAL] Bryant Jump Shot: Missed
20071030HOULAL, 6, 0:47:28, [HOU] Alston Rebound (Off:0 Def:1)
20071030HOULAL, 7, 0:47:06, [HOU] Yao Jump Shot: Missed
示例 2:
a1(away),a2(away), a3(away), a4, a5, h1, h2, h3, h4, h5, period,time,team, etype, assist, away, block, entered, home, left, num, opponent, outof, player, points,possession, reason, result, steal
(表示为 csv 文件中的每一行存储的值 - 太容易混淆而无法包含示例数据)
问题是这样的:
对于每支球队,都有一个球员名单。我的代码获取球员名单,然后只使用球员的姓氏创建一个名单,(这就是球员姓名的记录方式。)
第一个问题是在为每支球队累积场上的所有 10 名球员时出现的。
我使用累加器存储播放数据,直到记录所有 10 个播放器,然后写入播放数据,添加 10 个播放器。
问题是一些球员被“跳过”,当检查被指定为球员在场上的变量时。
例如,为休斯顿效力的 Luther Head 录制了一场比赛,但他从未被录制到a1, a2,
a3, a4
或a5
(当前比赛的客队)。
以下是我设置“检查”的方式,firstPlayer
作为记录播放的人并secondPlayer
作为记录的附加球员(例如助攻):
if team == homeTeam:
if h1 == ' ' and firstPlayer != h2 and firstPlayer != h3 and firstPlayer != h4 and firstPlayer != h5:
h1 = firstPlayer
if h2 == ' ' and firstPlayer != h1 and firstPlayer != h3 and firstPlayer != h4 and firstPlayer != h5:
h2 = firstPlayer
if h3 == ' ' and firstPlayer != h1 and firstPlayer != h2 and firstPlayer != h4 and firstPlayer != h5:
h3 = firstPlayer
if h4 == ' ' and firstPlayer != h1 and firstPlayer != h2 and firstPlayer != h3 and firstPlayer != h5:
h4 = firstPlayer
if h5 == ' ' and firstPlayer != h1 and firstPlayer != h2 and firstPlayer != h3 and firstPlayer != h4:
h5 = firstPlayer
if team == visitingTeam:
if a1 == ' ' and firstPlayer != a2 and firstPlayer != a3 and firstPlayer != a4 and firstPlayer != a5:
a1 = firstPlayer
if a2 == ' ' and firstPlayer != a1 and firstPlayer != a3 and firstPlayer != a4 and firstPlayer != a5:
a2 = firstPlayer
if a3 == ' ' and firstPlayer != a1 and firstPlayer != a2 and firstPlayer != a4 and firstPlayer != a5:
a3 = firstPlayer
if a4 == ' ' and firstPlayer != a1 and firstPlayer != a2 and firstPlayer != a3 and firstPlayer != a5:
a4 = firstPlayer
if a5 == ' ' and firstPlayer != a1 and firstPlayer != a2 and firstPlayer != a3 and firstPlayer != a4:
a5 = firstPlayer
if etype != 'sub': <<<<----This type of event is diffent
if team == homeTeam and secondPlayer != ' ':
if h1 == ' ' and secondPlayer != h2 and secondPlayer != h3 and secondPlayer != h4 and secondPlayer != h5:
h1 = secondPlayer
if h2 == ' ' and secondPlayer != h1 and secondPlayer != h3 and secondPlayer != h4 and secondPlayer != h5:
h2 = secondPlayer
if h3 == ' ' and secondPlayer != h1 and secondPlayer != h2 and secondPlayer != h4 and secondPlayer != h5:
h3 = secondPlayer
if h4 == ' ' and secondPlayer != h1 and secondPlayer != h2 and secondPlayer != h3 and secondPlayer != h5:
h4 = secondPlayer
if h5 == ' ' and secondPlayer != h1 and secondPlayer != h2 and secondPlayer != h3 and secondPlayer != h4:
h5 = secondPlayer
if team == visitingTeam and secondPlayer != ' ':
if a1 == ' ' and secondPlayer != a2 and secondPlayer != a3 and secondPlayer != a4 and secondPlayer != a5:
a1 = secondPlayer
if a2 == ' ' and secondPlayer != a1 and secondPlayer != a3 and secondPlayer != a4 and secondPlayer != a5:
a2 = secondPlayer
if a3 == ' ' and secondPlayer != a1 and secondPlayer != a2 and secondPlayer != a4 and secondPlayer != a5:
a3 = secondPlayer
if a4 == ' ' and secondPlayer != a1 and secondPlayer != a2 and secondPlayer != a3 and secondPlayer != a5:
a4 = secondPlayer
if a5 == ' ' and secondPlayer != a1 and secondPlayer != a2 and secondPlayer != a3 and secondPlayer != a4:
a5 = secondPlayer
我想这真的是我唯一的问题。
虽然,我想找到一种更好的方法来搜索字符串并获取信息。
为了获得玩家,我将字符串的前半部分分配给一个变量,将后半部分分配给另一个变量。然后我拆分字符串(sep
= ' ')
,所以它在一个列表中。
然后我检查每个字符串,看看是否有一个词是==
针对名单中的任何球员的,这取决于该比赛是为哪支球队录制的。
如果是第一个字符串,则为该玩家分配firstPlayer
变量,如果是第二个字符串,则为该玩家分配
secondPlayer
变量。
当我搜索etype
(事件类型)时,我只需使用以下表达式搜索字符串:
if 'Shot' in string:
etype = 'shot'
player = firstPlayer
if 'Assist' in string:
assist = secondPlayer
...等等各种事件类型。
如果您需要其他信息来帮助回答这个问题,请告诉我。