Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 那么,我如何通过调用已经创建的城市名称来获得城市的坐标和人口(将它们重新排列到列表中)?_Python_List_Indexing_Coordinates_Slice - Fatal编程技术网

Python 那么,我如何通过调用已经创建的城市名称来获得城市的坐标和人口(将它们重新排列到列表中)?

Python 那么,我如何通过调用已经创建的城市名称来获得城市的坐标和人口(将它们重新排列到列表中)?,python,list,indexing,coordinates,slice,Python,List,Indexing,Coordinates,Slice,这是数据文件的简化版本: Wichita, KS[3769,9734]279835 308 1002 1270 1068 1344 1360 1220 944 1192 748 1618 1774 416 1054 Wheeling, WV[4007,8072]43070 1017 1247 269 255 1513 327 589 203 1311 416 627 605 2442 998 85 West Palm Beach, FL[2672,8005]63305 1167 1550 1

这是数据文件的简化版本:

Wichita, KS[3769,9734]279835
308 1002 1270 1068 1344 1360 1220 944 1192 748 1618 1774 416 1054

Wheeling, WV[4007,8072]43070
1017 1247 269 255 1513 327 589 203 1311 416 627 605 2442 998 85

West Palm Beach, FL[2672,8005]63305
1167 1550 1432 965 1249 2375 1160 718 1048 2175 760 1515 1459 3280 1794 1252

Wenatchee, WA[4742,12032]17257
3250 2390 1783 1948 2487 2595 1009 2697 2904 2589 1394 2690 1765 2912 117 1461
2358

Weed, CA[4142,12239]2879
622 3229 2678 1842 1850 2717 2898 1473 2981 3128 2880 1858 2935 2213 3213 505
1752 2659

Waycross, GA[3122,8235]19371
2947 2890 360 820 1192 1097 605 904 2015 828 386 703 1815 413 1155 1127
2920 1434 899

Wausau, WI[4496,8964]32426
1240 2198 1725 1600 708 841 1138 805 913 848 1015 1222 907 646 1008 111
1230 1777 509 676

Waukegan, IL[4236,8783]67653
244 1000 2260 1933 1360 468 757 1023 565 673 1056 775 982 667 854 768
170 990 1985 551 436

Watertown, SD[4490,9711]15649
601 393 1549 1824 1351 1909 1058 572 880 1155 1263 534 1365 1572 1257 394
1358 433 1580 1403 156 1026
我目前可以从行中拆分城市和州名称。如何通过调用城市名称获得城市的坐标。我怎样才能通过叫出城市的名字来了解这个城市的人口
[x,y]
是坐标,
[]
后面的数字是人口

这是我现在的代码:

fin = open("miles.txt","r")
cities=[]
for line in fin:
    A=line.split()
    if A[0][0] not in '0123456789':
        B= A[0] + A[1][0]+ A[1][1]   
        cities.append[B]
print cities
谢谢!任何帮助都将不胜感激

def parse_cities(fin):
    cities = {}
    distances = {}
    order = []
    index = 0
    valid_city = False
    for line in fin:
        if '[' in line:  # city line
            # .partition() will always return three strings
            citystate, left, rest = line.partition('[')
            coordinates, right, population = rest.partition(']')
            lat, comma, lng = coordinates.partition(',')

            # drop any surrounding whitespace
            citystate = citystate.strip()
            lat = lat.strip()
            lng = lng.strip()
            population = population.strip()

            # validity check
            valid_city = (
                # delimiters must exist
                left and right and comma and
                # numbers must be valid
                lat.isdigit() and lng.isdigit() and population.isdigit()
            )

            if not valid_city: continue

            order.append(citystate)
            cities[citystate] = (int(lat), int(lng), int(population))
            index = 0
        elif line and line[0].isdigit() and valid_city:  # distance line
            # citystate variable from previous iteration
            # index variable will be reset for each new city,
            #     but otherwise increase between the lines
            for dist in [int(w) for w in line.split()]:
                if index >= len(order): break
                citystate2 = order[index]
                distances[citystate,citystate2] = dist
                distances[citystate2,citystate] = dist
                index += 1
    return cities, distances

准确,重复。是的…对不起,我只是不知道如何更改标题,所以我问了另一个…我完全理解。坚持这个网站。你会掌握窍门的。这里也复制一份:。他基本上一直在重复同一个问题。米扎德……非常感谢你的回答……我只有两个问题……如果我想创建两个不同的函数来分别返回人口、坐标呢?如果数据是这样的:Youngstown,哦[41108065]115436 Yankton,SD[42889739]12011966 Yakima,WA[466012051]49826 1513 2410马萨诸塞州伍斯特[42277180]161799 2964 1520 604威斯康星州德尔斯,威斯康星州[43638977]2521 1149 1817 481 595温斯顿塞勒姆,北卡罗来纳州[36108025]131885 927 729 2742 1289 494温尼伯,MB[49889715]564473 1611 686 1833 1446 1279温彻斯特,弗吉尼亚州[39197816]20217 1510 290 826 466 2641 1197 250威尔明顿,北卡罗来纳州[34247792]139238 390 1823 214 1139 765 2956 1500 637类似…966将是扬斯敦和坦克顿之间的距离…1513将是雅基玛到扬顿的距离,2410将是雅基玛到扬斯敦的距离…如何创建一个函数,该函数采用两个城市名称并返回两个城市之间的距离?谢谢……@MizardX
with open("miles.txt","r") as fin:
    cities, distances = parse_cities(fin)
print cities['Waycross, GA'] # (3122, 8235, 19371)
print distances['Youngstown, OH', 'Yakima, WA'] # 1513