简体   繁体   English

从字典列表中提取值以在 python 中进行计算?

[英]Extracting values from a list of dictionaries for calculations in python?

I have a list of dictionaries containing information about each of the London Boroughs.我有一个字典列表,其中包含有关每个伦敦自治市镇的信息。 I am supposed to determine the mean and median population, area, and population density of all of the boroughs.我应该确定所有行政区的平均和中位数人口、面积和人口密度。 I feel that perhaps the best way to do this would be to just assemble the population and area values into lists, but I don't know how.我觉得也许最好的方法是将人口和面积值组合到列表中,但我不知道如何。

Here's the data I imported, the file is called 'boroughs.'这是我导入的数据,文件名为“boroughs”。

[{'area': 13.93, 'name': 'Barking and Dagenham', 'population': 194352},
 {'area': 33.49, 'name': 'Barnet', 'population': 369088},
 {'area': 23.38, 'name': 'Bexley', 'population': 236687},
 {'area': 16.7, 'name': 'Brent', 'population': 317264},
 {'area': 57.97, 'name': 'Bromley', 'population': 317899},
 {'area': 8.4, 'name': 'Camden', 'population': 229719},
 {'area': 33.41, 'name': 'Croydon', 'population': 372752},
 {'area': 21.44, 'name': 'Ealing', 'population': 342494},
 {'area': 31.74, 'name': 'Enfield', 'population': 320524},
 {'area': 18.28, 'name': 'Greenwich', 'population': 264008},
 {'area': 7.36, 'name': 'Hackney', 'population': 257379},
 {'area': 6.33, 'name': 'Hammersmith and Fulham', 'population': 178685},
 {'area': 11.42, 'name': 'Haringey', 'population': 263386},
 {'area': 19.49, 'name': 'Harrow', 'population': 243372},
 {'area': 43.35, 'name': 'Havering', 'population': 242080},
 {'area': 44.67, 'name': 'Hillingdon', 'population': 286806},
 {'area': 21.61, 'name': 'Hounslow', 'population': 262407},
 {'area': 5.74, 'name': 'Islington', 'population': 215667},
 {'area': 4.68, 'name': 'Kensington and Chelsea', 'population': 155594},
 {'area': 14.38, 'name': 'Kingston upon Thames', 'population': 166793},
 {'area': 10.36, 'name': 'Lambeth', 'population': 314242},
 {'area': 13.57, 'name': 'Lewisham', 'population': 286180},
 {'area': 14.52, 'name': 'Merton', 'population': 203223},
 {'area': 13.98, 'name': 'Newham', 'population': 318227},
 {'area': 21.78, 'name': 'Redbridge', 'population': 288272},
 {'area': 22.17, 'name': 'Richmond upon Thames', 'population': 191365},
 {'area': 11.14, 'name': 'Southwark', 'population': 298464},
 {'area': 16.93, 'name': 'Sutton', 'population': 195914},
 {'area': 7.63, 'name': 'Tower Hamlets', 'population': 272890},
 {'area': 14.99, 'name': 'Waltham Forest', 'population': 265797},
 {'area': 13.23, 'name': 'Wandsworth', 'population': 310516},
 {'area': 8.29, 'name': 'Westminster', 'population': 226841},
 {'area': 1.12, 'name': 'City of London', 'population': 7000}]

So far I've tried to do a for loop, but it's not going well, and I keep printing out all of the population values but they can't be used to calculate anything because of the format they print in:到目前为止,我已经尝试做一个 for 循环,但它并不顺利,我一直打印出所有的人口值,但由于它们打印的格式,它们不能用于计算任何东西:

for item in boroughs:
     name = item['name']
     population = item['population']
     area = item['area']
     print(population)

The result looks like this .结果看起来像这样

I'm a beginner so please explain in very simple terms ):我是初学者,所以请用非常简单的术语解释):

It is easiest to use a pandas dataframe:使用pandas dataframe 是最简单的:

import pandas as pd
df = pd.DataFrame(boroughs)
df["density"] = df.population / df.area
print(df.describe())   # presents several useful statistics
print(df.area.mean())
print(df.density.median())

Alternatively use the standard library's statistics module:或者使用标准库的statistics模块:

from statistics import mean, median
print(mean(borough["area"] for borough in boroughs))
print(median(borough["population"] / borough["area"] for borough in boroughs)) # density

or similarly from numpy :或类似地来自numpy

from numpy import mean, median
print(median([borough["population"] for borough in boroughs])) 

There are previous questions with additional functions to calculate mean and median in case you don't want to use these modules for some reason.如果您出于某种原因不想使用这些模块,则之前有一些带有附加功能的问题可以计算平均值中位数

This is just a basic for loop.这只是一个基本的 for 循环。 If you have an int string,eg"123", make sure to convert with int如果您有一个 int 字符串,例如“123”,请确保使用int进行转换

total_pop = 0
for city in list:
    pop = int(city['population'])
    total_pop += pop

I think the following might work.我认为以下可能有效。 So as you said you want to form a list of populations and areas so that you can iterate over it to find the mean and median right?因此,正如您所说,您想形成一个人口和区域列表,以便您可以对其进行迭代以找到均值和中位数,对吗?

areas = []
population = []

for x in boroughs:
    areas.append(x['area'])
    population.append(x['population'])

So now you have the list of area and population and you can find the median and mean from it.所以现在你有了面积和人口的列表,你可以从中找到中位数和平均值。

EDIT: Now for the mean of the population, you can simply do the following.编辑:现在对于人口的平均值,您可以简单地执行以下操作。

mean_of_population = sum(population)/len(population)

And for Median对于中位数

n = len(population)
if n%2==0 : #ie if the number of element in the list population are even.
    median  = (population[n//2] + population[n//2 - 1])/2
else:
    median = population[n//2]

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

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