Python 收集数据、计数并返回字典列表,即使数据不存在

Python 收集数据、计数并返回字典列表,即使数据不存在,python,mysql,count,Python,Mysql,Count,假设我有一个像这样的mysql表 [ {name='cat', count=1}, {name='dog', count=0}, {name='fish', count=0}, {name='bird', count=1} ] [ {name='cat', count=0}, {name='dog', count=1}, {name='fish', count=1}, {name='bird', count=0} ] [ {name='cat', cou

假设我有一个像这样的mysql表

[
  {name='cat', count=1},
  {name='dog', count=0},
  {name='fish', count=0},
  {name='bird', count=1}
]
[
  {name='cat', count=0},
  {name='dog', count=1},
  {name='fish', count=1},
  {name='bird', count=0}
]
 [
  {name='cat', count=0},
  {name='dog', count=0},
  {name='fish', count=0},
  {name='bird', count=0}
]
tmp = dict()
for row in queryset:
   tmp.setdefault(row.customer, dict(cat=0, dog=0, fish=0, bird=0))
   if row.sub_type == 'cat':
      tmp[row.customer][row.sub_type] += 1
| id | type | sub | u type |客户|
|1 |动物|猫|约翰|
|2 |动物|狗|结婚|
|3 |动物|鱼|结婚|
|3 |动物|鸟|约翰|
我要做的是按客户收集数据,按子类型统计行数。动物类型有4个子类型(
),约翰有两个子类型(
),玛丽也有两个子类型(
)。假设我想得到John的一个结果,它应该是这样的

[
  {name='cat', count=1},
  {name='dog', count=0},
  {name='fish', count=0},
  {name='bird', count=1}
]
[
  {name='cat', count=0},
  {name='dog', count=1},
  {name='fish', count=1},
  {name='bird', count=0}
]
 [
  {name='cat', count=0},
  {name='dog', count=0},
  {name='fish', count=0},
  {name='bird', count=0}
]
tmp = dict()
for row in queryset:
   tmp.setdefault(row.customer, dict(cat=0, dog=0, fish=0, bird=0))
   if row.sub_type == 'cat':
      tmp[row.customer][row.sub_type] += 1
当我想得到关于
mary
的结果时,应该是这样的

[
  {name='cat', count=1},
  {name='dog', count=0},
  {name='fish', count=0},
  {name='bird', count=1}
]
[
  {name='cat', count=0},
  {name='dog', count=1},
  {name='fish', count=1},
  {name='bird', count=0}
]
 [
  {name='cat', count=0},
  {name='dog', count=0},
  {name='fish', count=0},
  {name='bird', count=0}
]
tmp = dict()
for row in queryset:
   tmp.setdefault(row.customer, dict(cat=0, dog=0, fish=0, bird=0))
   if row.sub_type == 'cat':
      tmp[row.customer][row.sub_type] += 1
因此,不在数据库中的sub_类型应该返回
count
为0。假设我想得到
Matthew
的结果。由于没有
Matthew
的数据,因此结果应该如下所示

[
  {name='cat', count=1},
  {name='dog', count=0},
  {name='fish', count=0},
  {name='bird', count=1}
]
[
  {name='cat', count=0},
  {name='dog', count=1},
  {name='fish', count=1},
  {name='bird', count=0}
]
 [
  {name='cat', count=0},
  {name='dog', count=0},
  {name='fish', count=0},
  {name='bird', count=0}
]
tmp = dict()
for row in queryset:
   tmp.setdefault(row.customer, dict(cat=0, dog=0, fish=0, bird=0))
   if row.sub_type == 'cat':
      tmp[row.customer][row.sub_type] += 1
我通常使用
setdefault()
生成结果。我的代码可能是这样的

[
  {name='cat', count=1},
  {name='dog', count=0},
  {name='fish', count=0},
  {name='bird', count=1}
]
[
  {name='cat', count=0},
  {name='dog', count=1},
  {name='fish', count=1},
  {name='bird', count=0}
]
 [
  {name='cat', count=0},
  {name='dog', count=0},
  {name='fish', count=0},
  {name='bird', count=0}
]
tmp = dict()
for row in queryset:
   tmp.setdefault(row.customer, dict(cat=0, dog=0, fish=0, bird=0))
   if row.sub_type == 'cat':
      tmp[row.customer][row.sub_type] += 1

但是,我想知道是否有其他方法或更优雅的方法来实现这一点。

假设您有一个名为“people”的表,其中包含包含条目的字段“name”

name
--------
John
Mary
Mathew
上面提到的表格是一张叫做“宠物”的表格

您可以使用以下查询为每个人构建结果集

select
  A.name as customer,
  (select count(*) from pets where customer=A.name and sub_type='cat') as cat,
  (select count(*) from pets where customer=A.name and sub_type='dog') as dog,
  (select count(*) from pets where customer=A.name and sub_type='fish') as fish,
  (select count(*) from pets where customer=A.name and sub_type='bird') as bird
from people A
结果如下所示

customer    cat     dog     fish    bird
John        1       0       0       1
Marry       0       1       1       0
Mathew      0       0       0       0
添加额外的where子句并筛选我的姓名或提供
一次汇总所有结果。

您介意使用
pandas
吗?答案可能取决于
mysql
表类型,因此我在您的问题中添加了该标记,它与使用通用Python无关。是的,我很想使用查询