gpt4 book ai didi

python - 从包含日期的列表中删除字符串,而不影响列表中的独立日期

转载 作者:太空宇宙 更新时间:2023-11-04 01:04:01 24 4
gpt4 key购买 nike

首先,对于冗长的标题感到抱歉。这是我的系统规范。 Windows 7 64 位,在 Pycharm 教育版 1.0.1 中运行 python 3.4.3 64 位

现在,进入问题。我有一个列表,其中包含从网站提取的数据。该列表包含字符串,有些只是日期,有些只是单词,有些是带单词的日期。它看起来像这样:

tempDates = ['Date', 'Visitor', 'Home', 'Notes', '2013-10-01', 'Washington Capitals', 'Chicago Blackhawks', None, '2013-10-01', 'Winnipeg Jets',..., 'St. Louis Blues',..., 'Postponed due to blizzard until 2014-02-25', etc]

我想做的是删除除独立日期之外的所有内容。使用生成器、while 循环和 if 语句,我能够删除除包含日期和单词的字符串之外的所有内容。该部分代码如下所示:

dates = []
d = 0
while d < len(tempDates):
if tempDates[d] is None or all(i.isalpha() or i == ' ' or i == ',' or i == '-' or i == '.' for i in tempDates[d]):
d += 1
else:
dates.append(tempDates[d])
d += 1

这段代码的输出是这样的:

dates = ['2013-10-01', '2013-10-01',..., '2014-01-21', '2014-01-21', '2014-01-21', 'Postponed due to snowstorm until 2014-01-22', '2014-01-22', 'Make-up game for snowstorm 2014-01-21',..., '2014-06-13']

在不删除独立日期的情况下,我找不到任何方法来删除同时包含单词和日期的字符串。我尝试更改程序从 tempDates 中排序日期的顺序,但这只会导致更多的无限循环和内存问题。如果有帮助,这里是完整的程序:

1    from bs4 import BeautifulSoup
2 import requests
3 import pandas as pd
4 import re
5
6 # create empty lists to hold the data pulled from the website
7 dateList = []
8 gameList = []
9 winnerList = []
10 loserList = []
11
12 year = 2014 #program is made to iterate through all available seasons since 1918, but is set to start at 2014 for quicker troubleshooting
13 while year < 2016: # stops year at 2015, as this is the last year stats are available
14 if year == 2005: # prevents an error from the program trying to load data from 2005, as that season was canceled
15 year += 1
16 else:
17 # pulls the whole page and puts it into r
18 r = requests.get('http://www.hockey-reference.com/leagues/NHL_{}_games.html'.format(year))
19 data = r.text
20
21 soup = BeautifulSoup(data, "lxml")
22 foundTeams = soup.find_all(href=re.compile("teams"))
23 teams = [link.string for link in foundTeams]
24 teams = teams[2:]
25
26 foundScores = soup.find_all(align=re.compile("right"))
27 tempScores = [link.string for link in foundScores]
28 tempScores = tempScores[2:]
29
30 foundDates = soup.find_all(align=re.compile("left"))
31 tempDates = [link.string for link in foundDates]
32
33 dates = []
34 d = 0
35 while d < len(tempDates):
36 if tempDates[d] is None or all(i.isalpha() or i == ' ' or i == ',' or i == '-' or i == '.' for i in tempDates[d]):
37 d += 1
38 else:
39 dates.append(tempDates[d])
40 d += 1
41
42 season = soup.find('h1')
43 season = [link.string for link in season]
44
45 games = len(teams) / 2
46 games = int(games)
47
48 # goes through the pulled data and saves it into lists to be written to a compiled file
49 x = 0
50 y = 0
51 while x < len(teams):
52 if x % 2 == 0:
53 if tempScores[x] is None or all(i.isalpha() or i == ' ' or i == ',' for i in tempScores[x]):
54 x += 2
55 else:
56 print(dates[y])
57 if tempScores[x] > tempScores[x+1]:
58 print("In game", y + 1)
59 print("The", teams[x], tempScores[x], "won against the", teams[x+1], tempScores[x+1])
60 winnerList.append(teams[x])
61 loserList.append(teams[x+1])
62 elif tempScores[x] < tempScores[x+1]:
63 print("In game", y + 1)
64 print("The", teams[x+1], tempScores[x+1], "won against the", teams[x], tempScores[x])
65 winnerList.append(teams[x+1])
66 loserList.append(teams[x])
67 dateList.append(dates[y])
68 gameList.append(y)
69 x += 1
70 y += 1
71 else:
72 x += 1
73 year += 1
74
75 # converts the compiled lists to data frames
76 dateList = pd.DataFrame(dateList)
77 gameList = pd.DataFrame(gameList)
78 winnerList = pd.DataFrame(winnerList)
79 loserList = pd.DataFrame(loserList)
80
81 # puts the data frames into one data frame
82 compiledStats = dateList
83 compiledStats['Game'] = gameList
84 compiledStats['Game Winner'] = winnerList
85 compiledStats['Game Loser'] = loserList
86
87 # rename the columns
88 compiledStats.columns = ['Date', 'Game', 'Game Winner', 'Game Loser']
89 # write to a new file
90 compiledStats.to_csv('CSV/Compiled_NHL_Stats2.0.csv', index=False)

最佳答案

tempDates = ['Date', 'Visitor', 'Home', 'Notes', '2013-10-01', 'Washington Capitals', 'Chicago Blackhawks', None, '2013-10-01', 'Winnipeg Jets','...', 'St. Louis Blues','...', 'Postponed due to blizzard until 2014-02-25', 'etc']
print [i for i in tempDates if re.match(r"\d{4}-\d{2}-\d{2}",str(i))]

这应该为你做

关于python - 从包含日期的列表中删除字符串,而不影响列表中的独立日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31461726/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com