在我的项目中,我正在网废UFC网站,以收集每个UFC运动员的总胜利、总损失和总平局。
这是我的代码的一部分,因为我希望分别去除总赢家、总损失和总平局:
import re
record = "10-7-3 (W-L-D)" #W = wins, L= Loss, D= Draws
char = "-"
record+="-"
totalwins = ""
totalloss = ""
totaldraws = ""
correctRecord = re.findall('\[[^\]]*\]|\([^\)]*\)|\"[^\"]*\"|\S+',record)[0]
print("The correct record per fighter is:", correctRecord)
totaldash = 0
for i in range(len(correctRecord)):
if(record[i] == char):
totaldash+=1
if totaldash == 1:
print("The total wins", totalwins)
totalwins =""
elif totaldash ==2:
print("The total losses ", totalwins)
totalwins=""
elif totaldash ==3:
print("The total draws ", totalwins)
elif (correctRecord[i] !=char):
totalwins +=correctRecord[i]结果如下:
The correct record per fighter is: 10-7-3
The total wins 10
The total losses 7问题是,我无法说出全部的平局。我还尝试使用条带方法,但没有结果:
correctRecord= str(record.split(separator, 1)[0])发布于 2022-09-15 01:38:28
尝试:
import re
record = "10-7-3 (W-L-D)"
wins, loss, draw = map(int, re.findall(r"\d+", record))
print(f"{wins=} {loss=} {draw=}")指纹:
wins=10 loss=7 draw=3发布于 2022-09-15 02:21:15
在数据以不同的顺序工作的情况下,会有一些更复杂的事情发生:
import re
record = "10-3-7 (W-D-L)"
values = re.findall(r'\d+|[WDL]', record)
vdict = dict(zip(values[3:], values[:3]))
print(f'wins={vdict["W"]}, losses={vdict["L"]}, draws={vdict["D"]}')输出:
wins=10, losses=7, draws=3https://stackoverflow.com/questions/73724666
复制相似问题