我以这种形式从MySQL得到了一些值
({'count(*)': 4739, 'catg': 17}, {'count(*)': 1629, 'catg': 145}, {'count(*)': 2683, 'catg': 8}, {'count(*)': 1526, 'catg': 10})
而且我正在尝试使字典输出像这样:
{17: 4739, 145: 1629, 8: 2683, 10:1526}
那有可能吗?
您可以做:
{element['catg']: element['count(*)'] for element in data}
示例:
In [51]: data = ({'count(*)': 4739, 'catg': 17}, {'count(*)': 1629, 'catg': 145}, {'count(*)': 2683, 'catg': 8}, {'count(*)': 1526, 'catg': 10})
In [52]: {element['catg']: element['count(*)'] for element in data}
Out[52]: {17: 4739, 145: 1629, 8: 2683, 10: 1526}