首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在django中根据字典中的数据计数分配颜色

在django中根据字典中的数据计数分配颜色
EN

Stack Overflow用户
提问于 2013-03-13 22:55:39
回答 1查看 78关注 0票数 2

从数据库中,我得到了下面的数据。然后,我将根据这些数据准备一个python字典。现在我想给每一行分配一种颜色,但有一个条件。如果一个codezone与多个zip相关联,那么该行将变为灰色。与单个zip关联的区域将获得一些其他颜色。我如何从下面的数据准备字典。

代码语言:javascript
运行
复制
+----------+-------+--------------+
| codeZone | ZipCd | Longitude    |
+----------+-------+--------------+
| Zone 81  | 13064 | -76.70275100 |
| Zone 81  | 13143 | -76.73114800 |
| Zone 81  | 13146 | -76.76016600 |
| Zone 72  | 13148 | -76.78488000 |
| Zone 72  | 13165 | -76.87704600 |
| Zone 50  | 14011 | -78.28090700 |
| Zone 50  | 14020 | -78.19062500 |
| Zone 50  | 14058 | -78.15694600 |
| Zone 50  | 14098 | -78.37735300 |
| Zone 50  | 14103 | -78.38546900 |
| Zone 50  | 14125 | -78.27249300 |
| Zone 50  | 14143 | -78.08089700 |
| Zone 50  | 14411 | -78.20223900 |
| Zone 81  | 14413 | -76.98321400 |
| Zone 60  | 14414 | -77.73609300 |
| Zone 50  | 14416 | -77.98543200 |
| Zone 72  | 14418 | -77.21132300 |
| Zone 14  | 14420 | -77.92950200 |
| Zone 50  | 14422 | -78.07160700 |
| Zone 60  | 14423 | -77.84307800 |
| Zone 71  | 14424 | -77.30122500 |
| Zone 70  | 14425 | -77.31024000 |
| Zone 61  | 14427 | -78.04682800 |
| Zone 16  | 14428 | -77.81500000 |
+----------+-------+--------------+

我的数据看起来像这样

{'zone':1,'zip':1,'spec':a},{'zone':1,'zip':2,'spec':m},{'zone':2,'zip':3,'spec':pC}

现在区域1与zip 1和zip 2关联。区域2只与zip 3关联。所以我想给dictioanry分配第四个键(即颜色),这将基于区域的计数(如果有多个区域,则为灰色)。因此,zip 1和zip 2的颜色将是灰色。任何其他颜色的zip 3。

EN

回答 1

Stack Overflow用户

发布于 2013-03-13 23:01:34

计算每个区域的邮政编码,然后将该信息包括在映射中:

代码语言:javascript
运行
复制
from collections import defaultdict

zipcount = defaultdict(set)
for zone, zip, longitude in datarows:
    zipcount[zone].add(zip)

# Create the output dictionary
output_dict = {}
for zone, zip, longitude in datarows:
    count = len(zipcount[zone])
    # adjust output dictionary based on the count (1 or more)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15388749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档