【问题标题】:Python: How to sort and organize dictionary dataPython:如何对字典数据进行排序和组织
【发布时间】:2015-12-02 07:11:27
【问题描述】:

我正在尝试按邮政编码对犯罪总数进行排序,并按犯罪类型对受害者人数进行排序。我已经按报告编号建立了字典。这是我在打印字典时输出的一小部分数据:

{'100065070': ['64130', '18', 'VIC', 'VIC', 'VIC'], '20003319': ['64130', '13', 'VIC'], '60077156': ['64130', '18', 'VIC'], '100057708': ['99999', '17', 'VIC', 'VIC'], '40024161': ['64108', '17', 'VIC', 'VIC']}

字典构建如下:{Report_number: [邮编、犯罪类型、受害者人数]}

我是编码新手,只是在学习字典。我将如何对字典进行排序以将我的数据组织成这种格式?

 Zip Codes Crime totals 

=====================

   64126 809
   64127 3983

   64128 1749
   64129 1037
   64130 4718
   64131 2080
   64132 2060
   64133 2005
   64134 2928

任何帮助将不胜感激。以下是我到目前为止的代码。我正在访问两个包含大约 50,000 行数据的文件,因此效率非常重要。

from collections import Counter

incidents_f =  open('incidents.csv', mode = "r")

crime_dict = dict()

for line in incidents_f:
    line_1st = line.strip().split(",")
    if line_1st[0].upper() != "REPORT_NO":
        report_no = line_1st[0]
        offense = line_1st[3]
        zip_code = line_1st[4]
        if len(zip_code) < 5:
            zip_code = "99999"

        if report_no in crime_dict:
            crime_dict[report_no].append(zip_code).append(offense)
        else:
            crime_dict[report_no] = [zip_code]+[offense]

#close File
incidents_f.close

details_f = open('details.csv',mode = 'r')
for line in details_f:
    line_1st = line.strip().split(",")
    if line_1st[0].upper() != "REPORT_NO":
        report_no = line_1st[0]
        involvement = line_1st[1]
        if involvement.upper() == 'VIC':
            victims = "VIC"

        if report_no in crime_dict:
            crime_dict[report_no].append(victims)
        else:
            continue


#close File
details_f.close



print(crime_dict)

【问题讨论】:

  • 如果您可以编辑问题以包含 CSV 文件中的几个示例行,这将有所帮助。

标签: python performance list csv dictionary


【解决方案1】:

这是一种比@Alexander 的解决方案使用更多代码的方法:

crime_dict ={
    '100065070': ['64130', '18', 'VIC', 'VIC', 'VIC'], 
    '20003319': ['64130', '13', 'VIC'], 
    '60077156': ['64130', '18', 'VIC'],
    '100057708': ['99999', '17', 'VIC', 'VIC'], 
    '40024161': ['64108', '17', 'VIC', 'VIC']
    }

crimes_by_zip = {}
for k, v in crime_dict.items():
    zip = v[0]
    if zip not in crimes_by_zip.keys():
        crimes_by_zip[zip] = 0
    crimes_by_zip[zip] += 1

for zip in sorted(crimes_by_zip.keys()):
    print(zip, crimes_by_zip[zip])

64108 1
64130 3
99999 1

【讨论】:

  • 谢谢史蒂夫。这完美地工作并且完全有意义。感谢您的帮助。
【解决方案2】:
D = {'100065070': ['64130', '18', 'VIC', 'VIC', 'VIC'], '20003319': ['64130', '13', 'VIC'], '60077156': ['64130', '18', 'VIC'], '100057708': ['99999', '17', 'VIC', 'VIC'], '40024161': ['64108', '17', 'VIC', 'VIC']}

data_with_zip_duplicate = [(D[key][0],key) for  key in sorted(D.keys(), key = lambda x:D[x][0] )]
print(*data_with_zip_duplicate, sep = "\n")

【讨论】:

  • 感谢您的帮助。我们还没有接触到 Lambda 函数,但是在查找这个问题时,我到处都看到了这些解决方案。我很好奇,lambda 函数是否比史蒂夫建议的 zip 解决方案更有效?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 2020-11-02
  • 1970-01-01
  • 1970-01-01
  • 2013-02-21
  • 2014-11-10
相关资源
最近更新 更多