繁体   English   中英

Python 中的字典中的一个键具有多个值

[英]Having Multiple Values to One Key in a Dictionary in Python

我在向一个键添加多个值时遇到问题,我希望我的字典有一个“县”键,并且每个县的值与我的大学城市字典中的值一样多。 提前致谢!

from geopy.distance import distance

college_towns = {

    'Stillwater': (36.125074,  -97.072201),
    'Norman': (35.194525, -97.444354),
    'Edmond': (35.656744, -97.470951)

}

counties = {

    'Adair':    (35.8975,   -94.651671),
    'Alfalfa':  (36.729703, -98.323445),
    'Atoka':    (34.374813, -96.034705),
    'Beaver':   (36.748334, -100.483056),
    'Beckham':  (35.273945, -99.671638),
    'Blaine':   (35.877782, -98.428934),
    'Bryan':    (33.964004, -96.264137),
    'Caddo':    (35.16792,  -98.381045),
    'Canadian': (35.543416, -97.979836),
    'Carter':   (34.251848, -97.287927),
    'Cherokee': (35.904367, -94.996796),
    'Choctaw':  (34.027645, -95.554208),
    'Cimarron': (36.755276, -102.508735),
    'Cleveland':    (35.203117, -97.328332),
    'Coal': (34.582861, -96.288039),
    'Comanche': (34.662628, -98.476597),
    'Cotton':   (34.290676, -98.373438),
    'Craig':    (36.76389,  -95.201553),
    'Creek':    (35.907732, -96.379793),
    'Custer':   (35.645601, -98.997386),
    'Delaware': (36.393369, -94.808206),
    'Dewey':    (35.978433, -99.014094),
    'Ellis':    (36.224258, -99.750139),
    'Garfield': (36.378273, -97.787729),
    'Garvin':   (34.70935,  -97.312723),
    'Grady':    (35.021058, -97.88689),
    'Grant':    (36.788254, -97.788151),
    'Greer':    (34.935263, -99.552968),
    'Harmon':   (34.745971, -99.844194),
    'Harper':   (36.800456, -99.662842),
    'Haskell':  (35.232294, -95.109578),
    'Hughes':   (35.052934, -96.251183),
    'Jackson':  (34.593949, -99.41221),
    'Jefferson':    (34.105092, -97.838814),
    'Johnston': (34.313455, -96.654255),
    'Kay':  (36.814842, -97.143755),
    'Kingfisher':   (35.949431, -97.934568),
    'Kiowa':    (34.921489, -98.981617),
    'Latimer':  (34.875137, -95.272263),
    'Le Flore': (34.899642, -94.703491),
    'Lincoln':  (35.703118, -96.881392),
    'Logan':    (35.914151, -97.450764),
    'Love': (33.957775, -97.245124),
    'McClain':  (35.016414, -97.449811),
    'McCurtain':    (34.117073, -94.766086),
    'McIntosh': (35.369092, -95.671764),
    'Major':    (36.313119, -98.542015),
    'Marshall': (34.027007, -96.770533),
    'Mayes':    (36.303804, -95.235638),
    'Murray':   (34.485766, -97.071557),
    'Muskogee': (35.617551, -95.383911),
    'Noble':    (36.384901, -97.236335),
    'Nowata':   (36.789615, -95.613312),
    'Okfuskee': (35.466804, -96.327762),
    'Oklahoma': (35.554611, -97.409401),
    'Okmulgee': (35.646879, -95.96458),
    'Osage':    (36.62468,  -96.408385),
    'Ottawa':   (36.835764, -94.802681),
    'Pawnee':   (36.313704, -96.696669),
    'Payne':    (36.079225, -96.975255),
    'Pittsburg':    (34.92554,  -95.74813),
    'Pontotoc': (34.721071, -96.692738),
    'Pottawatomie': (35.211393, -96.957007),
    'Pushmataha':   (34.377896, -95.408085),
    'Roger Mills':  (35.708554, -99.741572),
    'Rogers':   (36.378082, -95.601337),
    'Seminole': (35.158366, -96.602858),
    'Sequoyah': (35.502435, -94.750757),
    'Stephens': (34.481361, -97.855607),
    'Texas':    (36.741964, -101.488434),
    'Tillman':  (34.371085, -98.931701),
    'Tulsa':    (36.120121, -95.941731),
    'Wagoner':  (35.963479, -95.5141),
    'Washington':   (36.70438,  -95.906155),
    'Washita':  (35.290177, -98.991962),
    'Woods':    (36.765141, -98.868967),
    'Woodward': (36.425619, -99.273661)

}
for city, coords in college_towns.items():
    d = {
        'county': coords
    }

for county, coord in counties.items():
    for city, coords in college_towns.items():
        d[county] = distance(coord, coords).miles
        print(county, d[county])

我目前的 output 是:

Adair 136.4922891469114
Adair 164.6391572270637
Adair 159.25535744806874
Alfalfa 81.23353285605839
Alfalfa 116.75075510750989
Alfalfa 87.9928638267299
Atoka 134.16552438092384
Atoka 98.07963763791889
Atoka 120.18011702380218
.....

我希望它列出县,然后列出相应的三个距离值,而不是重复三次。

使用带有listdefaultdict作为默认工厂,然后 append 每个距离。

dd = defaultdict(list)
for county, coord in counties.items():
    for city, coords in college_towns.items():
        dd[county].append(distance(coord, coords).miles)

>>> dict(dd)
{'Adair': [136.4922891469114, 164.6391572270637, 159.25535744806874],
 'Alfalfa': [81.23353285605839, 116.75075510750989, 87.9928638267299],
 'Atoka': [134.1655243809232, 98.07963763791824, 120.18011702380221],
 ...}
d = {
    county: {
        town: distance(county_pos, town_pos).miles
            for town, town_pos in college_towns.items()
    } for county, county_pos in counties.items()
}

d的键是县,内部词典的键是大学城。 您可以像这样访问:

d['Adair']['Norman'] # distance Adair-Norman

我希望它列出县,然后列出相应的三个距离值,而不是重复三次。

如果这是您唯一需要做的不同的事情,那么问题的标题有点误导。 您可以重新排列打印的 output

for county, coord in counties.items():
    print(county, " distance to")
    for city, coords in college_towns.items():
        d[county] = distance(coord, coords).miles
        print(" " * 4, city, " is ", d[county], " miles")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM