我在列表中有一个字典,结构如下:
my_list = [
{
"id" : 1,
"name" : "foo",
"address" : "here"
},
{
"id" : 2,
"name" : "foo2",
"address" : "there"
},
{
"id" : 3,
"name" : "foo3",
"address" : "there"
},
]
如何获取特定地址的总数?就像我想知道地址“那里”有多少人一样。我该怎么做??
请您参考如下方法:
将 len 函数与 list_comprehension 一起使用。
>>> my_list = [
{
id : 1,
'name' : 'foo',
'address' : 'here'
},
{
id : 2,
'name' : 'foo2',
'address' : 'there'
},
{
id : 3,
'name' : 'foo3',
'address' : 'there'
},
]
>>> len([x for x in my_list if x['address'] == 'there'])
2
