繁体   English   中英

在python中嵌套和更新字典

[英]Nesting and updating dictonaries in python

我有一个文件(census.txt),我试图将其抽象为嵌套字典。 为了便于编程,该文件已复制到代码中。 我遇到的问题是,我无法以某种方式迭代和“更新”字典。 请你放点灯好吗?

如果有人可以提供一些关于更新字典的提示,我想进一步尝试..这是我所拥有的:

#!/usr/bin/python
#
#
#######   census.txt   #################
#day0   nameOfCity  count   records 
#day0   city0       1   2       
#day0   city1       5   6       
#day0   city2       6   12      
#day1   nameOfCity  count   records 
#day1   city0       1   2       
#day1   city1       7   5       
#day1   city2       6   12      
#day2   nameOfCity  count   records 
#day2   city0       1   2       
#day2   city1       7   5       
#day2   city2       6   12      
#day2   city0       4   3
#######################################
#
#Note: # sum values of data for same city entered multiple times
#
#####  REQUIRED NESTED DICTIONARY OUTPUT  ###############
#
#{ day0: {city0 : {'count': 1, 'records' : 2}, city1: {'count' : 5, 'records' : 6}, city2: {'count' : 6, 'records' : 12}},
#  day1: {city0 : {'count': 1, 'records' : 2}, city1: {'count' : 7, 'records' : 5}, city2: {'count' : 6, 'records' : 12}},
#  day2: {city0 : {'count': 5, 'records' : 5}, city1: {'count' : 7, 'records' : 5}, city2: {'count' : 6, 'records' : 12}} }
#  
# #######################################################


import sys
import os

# ===================
# Main Python section
# ===================
if __name__ == '__main__':

    omit_list =['nameOfCity'] # omit the heading line each time and pick only values
    count_rows = 0 
    count_columns =  4 #known beforehand, 
    items = [0 for i in range (count_columns)]
    dict2 = {} #outermost dictionary with day as key
    dict1 = {} #inner dictionary with city names as keys
    dict0 = {} #innermost dictionary with count and records as keys
    with open('census.txt', 'r') as fname:
    for line in fname:
        if (not any(omitted_word in line for omitted_word in omit_list)) and line.strip():
            items =line.split()
            if len(items) == count_columns:
                dict0["count"]=items[2]
                dict0["records"]=items[3] 
                dict1[items[1]]= dict0 
                if items[0] not in dict2:
                        dict2[items[0]] = dict1
                        print 'if'
                else:
                    dict2[items[0]].update(dict1)
                    print 'else'    
                dict0 = {}
                print dict2
                count_rows +=1              
    #print count_rows
    print "*********** dict2 ************"
    print dict2  
    fname.close()

我试图抽象出每个城市的每日信息。 这里编码时的第一个问题是,例如第 2 天的 city0 已被多次输入。

我需要在“创建”最里面的记录字典和计数之前总结记录数和计数,因为关键字段“city0”是唯一的。 另一件事是,“day”键是唯一的,但包含许多城市字段值。 不知何故,我无法实现这种嵌套和代码的输出:

*********** dict2 ************
{'day2': {'city2': {'count': '6', 'records': '12'}, 'city0': {'count': '4', 'records': '3'}, 'city1': {'count': '7', 'records': '5'}}, 'day0': {'city2': {'count': '6', 'records': '12'}, 'city0': {'count': '4', 'records': '3'}, 'city1': {'count': '7', 'records': '5'}}, 'day1': {'city2': {'count': '6', 'records': '12'}, 'city0': {'count': '4', 'records': '3'}, 'city1': {'count': '7', 'records': '5'}}}

(这显然是错误的,因为只有最里面的字典才被第 2 天的信息覆盖)

我期望的实际输出是:

{ day0: {city0 : {'count': 1, 'records' : 2}, city1: {'count' : 5, 'records' : 6}, city2: {'count' : 6, 'records' : 12}},
  day1: {city0 : {'count': 1, 'records' : 2}, city1: {'count' : 7, 'records' : 5}, city2: {'count' : 6, 'records' : 12}},
  day2: {city0 : {'count': 5, 'records' : 5}, city1: {'count' : 7, 'records' : 5}, city2: {'count' : 6, 'records' : 12}} }

如果您的文件一致,以下应该有效:

days = {}

current_day = None
with open('census.txt') as fname:
    for l in fname:
        day, city, count, records = l.split()
        if current_day != day:
            current_day = day
            days[current_day] = {}
        else:
            count = int(count)
            records = int(records)
            if city in days[current_day]:
                days[current_day][city]['count'] += count
                days[current_day][city]['records'] += records
            else:
                days[current_day][city] = {'count': count, 'records': records}

print(days)

另请注意:如果使用with语句,则不需要.close()文件。 它会自动关闭。

感谢上面帖子中的代码。 这对我有用:

days = {}
omit_list =['nameOf'] # omit the heading line each time and pick only values
current_day = None
with open('census.txt') as fname:
    for l in fname:
    if (not any(omitted_word in l for omitted_word in omit_list)) and l.strip():
        day, city, count, records = l.split()
        count = int(count)
        records = int(records)
        if current_day != day:
            current_day = day
            days[current_day] = {}
            days[current_day][city] = {'count': count, 'records': records}
        else:
            if city in days[current_day]:
            days[current_day][city]['count'] += count
            days[current_day][city]['records'] += records
            else:
            days[current_day][city] = {'count': count, 'records': records}

print(days)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM