我有一个如下元素的Python列表:
['Alabama[edit]',
'Auburn (Auburn University)[1]',
'Florence (University of North Alabama)',
'Jacksonville (Jacksonville State University)[2]',
'Livingston (University of West Alabama)[2]',
'Montevallo (University of Montevallo)[2]',
'Troy (Troy University)[2]',
'Tuscaloosa (University of Alabama, Stillman College, Shelton State)[3][4]',
'Tuskegee (Tuskegee University)[5]',
'Alaska[edit]',
'Fairbanks (University of Alaska Fairbanks)[2]',
'Arizona[edit]',
'Flagstaff (Northern Arizona University)[6]',
'Tempe (Arizona State University)',
'Tucson (University of Arizona)',
'Arkansas[edit]',
'Arkadelphia (Henderson State University, Ouachita Baptist University)[2]',
'Conway (Central Baptist College, Hendrix College, University of Central Arkansas)[2]',
'Fayetteville (University of Arkansas)[7]']
这个清单并不完整,但足以让你了解其中的内容。在
数据结构如下:
有一个美国州的名字,在州名后面有一些城市的名字。如您所见,州名以“[edit]”结尾,城市名称要么在括号中以数字结尾(例如“1”或“[2]”),要么在括号内加上大学名称(例如“(北阿拉巴马大学)”。在
(查找此问题的完整引用文件here)
理想情况下,我需要一个以州名称为索引的Python字典,并将该州的所有城市名称嵌套列出作为特定索引的值。例如,字典应该是这样的:
^{pr2}$现在,我尝试了以下解决方案,去掉不必要的部分:
import numpy as np
import pandas as pd
def get_list_of_university_towns():
'''
Returns a DataFrame of towns and the states they are in from the
university_towns.txt list. The format of the DataFrame should be:
DataFrame( [ ["Michigan", "Ann Arbor"], ["Michigan", "Yipsilanti"] ],
columns=["State", "RegionName"] )
The following cleaning needs to be done:
1. For "State", removing characters from "[" to the end.
2. For "RegionName", when applicable, removing every character from " (" to the end.
3. Depending on how you read the data, you may need to remove newline character '\n'.
'''
fhandle = open("university_towns.txt")
ftext = fhandle.read().split("\n")
reftext = list()
for item in ftext:
reftext.append(item.split(" ")[0])
#pos = reftext[0].find("[")
#reftext[0] = reftext[0][:pos]
towns = list()
dic = dict()
for item in reftext:
if item == "Alabama[edit]":
state = "Alabama"
elif item.endswith("[edit]"):
dic[state] = towns
towns = list()
pos = item.find("[")
item = item[:pos]
state = item
else:
towns.append(item)
return ftext
get_list_of_university_towns()
代码生成的输出片段如下所示:
{'Alabama': ['Auburn',
'Florence',
'Jacksonville',
'Livingston',
'Montevallo',
'Troy',
'Tuscaloosa',
'Tuskegee'],
'Alaska': ['Fairbanks'],
'Arizona': ['Flagstaff', 'Tempe', 'Tucson'],
'Arkansas': ['Arkadelphia',
'Conway',
'Fayetteville',
'Jonesboro',
'Magnolia',
'Monticello',
'Russellville',
'Searcy'],
'California': ['Angwin',
'Arcata',
'Berkeley',
'Chico',
'Claremont',
'Cotati',
'Davis',
'Irvine',
'Isla',
'University',
'Merced',
'Orange',
'Palo',
'Pomona',
'Redlands',
'Riverside',
'Sacramento',
'University',
'San',
'San',
'Santa',
'Santa',
'Turlock',
'Westwood,',
'Whittier'],
'Colorado': ['Alamosa',
'Boulder',
'Durango',
'Fort',
'Golden',
'Grand',
'Greeley',
'Gunnison',
'Pueblo,'],
'Connecticut': ['Fairfield',
'Middletown',
'New',
'New',
'New',
'Storrs',
'Willimantic'],
'Delaware': ['Dover', 'Newark'],
'Florida': ['Ave',
'Boca',
'Coral',
'DeLand',
'Estero',
'Gainesville',
'Orlando',
'Sarasota',
'St.',
'St.',
'Tallahassee',
'Tampa'],
'Georgia': ['Albany',
'Athens',
'Atlanta',
'Carrollton',
'Demorest',
'Fort',
'Kennesaw',
'Milledgeville',
'Mount',
'Oxford',
'Rome',
'Savannah',
'Statesboro',
'Valdosta',
'Waleska',
'Young'],
'Hawaii': ['Manoa'],
但是,输出中有一个错误:名称中带有空格的州(如“北卡罗来纳州”)不包括在内。我能说出背后的原因。在
我曾想过使用正则表达式,但由于我还没有研究过正则表达式,我不知道如何形成正则表达式。有没有关于使用或不使用Regex的方法?在
你应该改变
无正则表达式解决方案:
^{pr2}$收益率(重新格式化):
^{3}$赞美正则表达式的力量吧:
这就产生了
^{pr2}$说明:
这样做的目的是将任务分成几个较小的任务:
第一个子任务 ^{3}$
第二个子任务
见the demo on regex101.com。在
第三个子任务
见another demo on regex101.com。在
第四个子任务
这大致相当于:
最后,一些时间问题:
因此,在我的电脑上运行上述a100000次需要大约12秒,所以它应该相当快。在
让我们一步一步解决你的问题:
收集所有的数据,这里我使用的是在任何状态名称出现时放置一个跟踪字,它会在单词“pos_flag”的帮助下跟踪和分块:
它将产生如下输出:
^{pr2}$正如你在每个州名之前看到的那样,现在让我们用这个词来做一些事情:
跟踪列表中所有“pos_flag words”的索引:
^{3}$这将产生如下输出:
我们现在有了索引号,我们可以用这些索引号来链接:
使用index no将列表分块,并将第一个单词设置为dict键,将其余单词设置为dict值:
输出:
由于dict在python 3.5中没有排序,因此输出顺序与输入文件不同:
完整代码:
如果要使用regex,请尝试以下小解决方案:
它将提供:
demo :
相关问题 更多 >
编程相关推荐