分析地图信息的python小程序

import re import string #get data to list #list[id,name,zuobiao,population,[juli1,juli2,...]] f=open("mapdata.txt","r") reg=re.compile(r"^\d|\s$")#check line regname=re.compile(r"(\w*)") regzuobiao=re.compile(r"(\w{2}\[.*\])") regpopu=re.compile(r"(?<=])\d*") listinfo=[] i=1 j=0 for line in f: #check data of this line if(reg.match(line)==None): li=[] li.append(i) li.append(regname.search(line).group(0)) li.append(regzuobiao.search(line).group(0)) li.append(regpopu.search(line).group(0)) listinfo.append(li) i+=1 else: if(j!=i): li.append(re.split("\s",line)) j=i else: li[4]+=re.split("\s",line) #print(listinfo) f.close() def convertStr(s): """Convert string to either int or float.""" try: ret = int(s) except ValueError: #Try float. ret = float(s) return ret #city->zuobiao def coordinate(city): for li in listinfo: #print(li[0]) if(li[1]==city): print(li[2]) #city->population def population(city): for li in listinfo: if(li[1]==city): print(li[3]) #city->distance def distance(city1,city2): for li in listinfo: if(li[1]==city1): c1=li if(li[1]==city2): c2=li if(c1[0]>c2[0]): print(c1[4][c2[0]-1]) else: print(c2[4][c1[0]-1]) def nearby(city,r): near=[] for li in listinfo: if(li[1]==city): c=li #up #print(type(int(c[3][1]))) i=0 for j in c[4]: i+=1 #e=int(j) k,m=0,0 i=i-2 while(k<=i): #print(int(c[3][k])) if(int(c[4][k])0): near.append(listinfo[c[0]-1-m][1]) m-=1 #down #print(c[0]) for li in listinfo: if(li[0]>c[0]): if(int(li[4][c[0]-1])<=r): #pass near.append(li[1]) print(near) #coordinate("Waco") #distance("Worcester","Yakima") population("Waco") nearby("Yakima",300)

页面