python编程问题,编写关于list的问题
发布网友
发布时间:2022-05-10 10:57
我来回答
共2个回答
热心网友
时间:2022-04-18 14:34
没有从网页上读取,把信息数据写到了mapdata.txt的文件了,星号注释的行没有复制
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])<r):
#print(int(c[3][k]))
m+=1
k+=1
while(m>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)
热心网友
时间:2022-04-18 15:52
#!/usr/bin/python
# encoding: utf-8
import codecs
import re
class City:
def __init__(self, **kwg):
self.__dict__ = kwg
self.people = int(self.__dict__.get('people'))
def __str__(self):
return '[%s]%s (%s) -- people:%d'%(
self.state, self.name, self.position, self.people,
)
patt = re.compile(r'''
(?P<name>[^,]+),\s+?
(?P<state>[A-Z]{2})
\[(?P<position>\d+\,\d+)\]
(?P<people>\d+)
''', re.I|re.U|re.X|re.M)
dpatt = re.compile('\d+', re.I)
cities, citylist, dists = {}, [], {}
def readdatafile(fname, coding='utf-8'):
''' data file parser '''
global cities, citylist, dists
city = None
with codecs.open(fname,'r', coding) as handle:
for ln in handle:
m = patt.match(ln.strip())
if m:
city = City(**m.groupdict())
city.ds = []
cities[city.name] = city
citylist.append(city.name)
elif city:
city.ds.extend(map(int, dpatt.findall(ln)))
for cityname in citylist:
city = cities[cityname]
for another, dist in zip(citylist, city.ds):
dists[tuple((cityname, another))] = dist
def coordinate(cityname):
city = cities.get(cityname)
return None if not city else city.position
def population(cityname):
city = cities.get(cityname)
return None if not city else city.people
def distance(c1, c2):
return dists.get(tuple((c1,c2)),
dists.get(tuple((c2,c1)))
)
def nearby(cityname, r):
return [c for c in citylist
if c!=cityname and distance(cityname, c)<r]
# prepair distance data
readdatafile(r'miles.dat')
print coordinate(u'Washington')
print population(u'Washington')
print distance(u'Worcester', u'Washington')
print nearby(u'Washington', 200)