我是python的新手,所以如果直截了当,我道歉。其他问题(here和here)已经解决了这些问题,但我还没有能够解决这个问题。
我有每个地理区域的dicts列表:
public class MyAuthorizationServerProvider: OAuthAuthorizationServerProvider
{
ICrendentials objcredential = new Crendentials();
LoginCrediential objUser = new LoginCrediential();
public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.Validated(); //
}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
if (context.UserName == objUser.UserName && context.Password == objUser.Password)
{
var x = objcredential.EmailValidation(context.UserName, context.Password);
if (x==true)
{
}
}
else
{
context.SetError("invalid_grant", "Provided username and password is incorrect");
return;
}
}
}
我只想通过将list_of_dicts = [{'id': 'a', 'population': '20', 'area': '10'},
{'id': 'a', 'population': '20', 'area': '10'}]
除以'population': 'value'
来计算每个区域的人口密度。此计算应创建一个新项目。
结果应如下所示:
'area': 'value'
答案 0 :(得分:3)
您可以简单地遍历每个字典,并将'pop_density'
与人口密度相关联:
for v in list_of_dicts:
v['pop_density'] = float(v['population'])/float(v['area'])
我们需要使用float(..)
将字符串'20'
转换为数字20
。如果所有值都是整数,我们可以使用int(..)
。但也许使用花车更安全。
如果您想创建list_of_dicts
的副本,可以使用列表理解:
[dict(v,pop_density=float(v['population'])/float(v['area'])) for v in list_of_dicts]
生成:
>>> [dict(v,pop_density=float(v['population'])/float(v['area'])) for v in list_of_dicts]
[{'population': '20', 'area': '10', 'pop_density': 2.0, 'id': 'a'}, {'population': '20', 'area': '10', 'pop_density': 2.0, 'id': 'a'}]
答案 1 :(得分:2)
您可以简单地遍历您的词典列表和计算。确保对结果进行舍入,因为您需要一个整数:
>>> list_of_dicts = [{'id': 'a', 'population': '20', 'area': '10'},
{'id': 'a', 'population': '20', 'area': '10'}]
>>>
>>> for d in list_of_dicts:
d['pop_density'] = int(d['population']) // int(d['area']) # round result by using //
>>> list_of_dicts
[{'pop_density': 2, 'population': '20', 'id': 'a', 'area': '10'}, {'pop_density': 2, 'population': '20', 'id': 'a', 'area': '10'}]
>>>
Python 3
如果您想要新词典列表,可以使用列表推导:
>>> list_of_dicts = [{'id': 'a', 'population': '20', 'area': '10'},
{'id': 'a', 'population': '20', 'area': '10'}]
>>>
>>> [{'pop_densitiy': int(d['population']) // int(d['area']), **d} for d in list_of_dicts]
[{'area': '10', 'population': '20', 'id': 'a', 'pop_densitiy': 2}, {'area': '10', 'population': '20', 'id': 'a', 'pop_densitiy': 2}]
>>>
Python 2
注意上面使用了仅在python 3中可用的字典解包操作符。如果使用Python 2,则需要使用dict
构造函数:
>>> list_of_dicts = [{'id': 'a', 'population': '20', 'area': '10'},
{'id': 'a', 'population': '20', 'area': '10'}]
>>> [dict(d, pop_densitiy=int(d['population']) // int(d['area'])) for d in list_of_dicts]
[{'pop_densitiy': 2, 'population': '20', 'id': 'a', 'area': '10'}, {'pop_densitiy': 2, 'population': '20', 'id': 'a', 'area': '10'}]
>>>
答案 2 :(得分:0)
只需迭代现有索引并为其添加新索引:
(::) is expecting the right side to be a:
List (List String)
But the right side is:
String
答案 3 :(得分:0)
list_of_dicts = [{'id': 'a', 'population': '20', 'area': '10'},
{'id': 'a', 'population': '30', 'area': '5'}]
[dict(j) for j in [ list(i.items()) + [ ('pop_density', int(i['population'])/int(i['area'])) ] for i in list_of_dicts ] ]
输出:
[{'area': '10', 'id': 'a', 'pop_density': 2.0, 'population': '20'},
{'area': '5', 'id': 'a', 'pop_density': 6.0, 'population': '30'}]