嵌套字典麻烦 [英] Nested dictionaries trouble

查看:53
本文介绍了嵌套字典麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我正在编写一个简单的FTP日志解析器,它在运行时对文件大小求和。我

有一个yearTotals字典,年份密钥和monthTotals

字典作为其值。 monthTotals字典有月份键

和文件大小值。该脚本的工作原理除外,所有年份的结果都写成了b $ b,而不是仅仅一年。我在想我设置词典或引用它们的方式有一个

错误...


import glob,traceback


years = [" 2005"," 2006"," 2007"]

months = [" 01"," 02", 03,04,05,06,07,08,09,10,11,12,

#创建月份字典以转换日志值

logMonths =

{" Jan":" 01"," Feb": " 02"," MAR":" 03","四月":" 04","五月" :05,Jun:06,Jul:07,Aug:08,Sep:09,Oct,Oct:J。 :10,Nov:11,Dec:12}

#创建具有默认值0的monthTotals字典

monthTotals = dict.fromkeys(月,0)

#在yearTotals字典中嵌套monthTotals字典

yearTotals = {}

年份:

yearTotals.setdefault(year,monthTotals)

currentLogs = glob.glob(" / logs / ftp / *")


尝试:
当前日志当前日志的


readLog = open(currentLog," r")

for readLog.readlines()中的行:

如果不是行:continue

if len(line)< 50:继续

logLine = line.split()


#第二个元素是月,第五个是年,第八个是文件大小

#从零开始计数:


#查找年/月对值

logMonth = logMonths [logLine [1]]

currentYearMonth = yearTotals [logLine [4]] [logMonth]


#更新年/月值

currentYearMonth + = int(logLine [7])

yearTotals [logLine [4]] [logMonth] = currentYearMonth

除外:

print" Failed on:" + currentLog

traceback.print_exc()


#打印词典

for yearTotals.keys():

print" KEY",''\ t''," VALUE"

print x,''\ t'',yearTotals [x]

#print" key,,\ t,价值

年份中的年份总计[x] .keys():

print" ,y,''\ t'',yearTotals [x] [y]

谢谢,

Ian

Hello,

I''m writing a simple FTP log parser that sums file sizes as it runs. I
have a yearTotals dictionary with year keys and the monthTotals
dictionary as its values. The monthTotals dictionary has month keys
and file size values. The script works except the results are written
for all years, rather than just one year. I''m thinking there''s an
error in the way I set my dictionaries up or reference them...

import glob, traceback

years = ["2005", "2006", "2007"]
months = ["01","02","03","04","05","06","07","08","09","10", "11","12"]
# Create months dictionary to convert log values
logMonths =
{"Jan":"01","Feb":"02","Mar":"03","Apr":"04","May" :"05","Jun":"06","Jul":"07","Aug":"08","Sep":"09", "Oct":"10","Nov":"11","Dec":"12"}
# Create monthTotals dictionary with default 0 value
monthTotals = dict.fromkeys(months, 0)
# Nest monthTotals dictionary in yearTotals dictionary
yearTotals = {}
for year in years:
yearTotals.setdefault(year, monthTotals)

currentLogs = glob.glob("/logs/ftp/*")

try:
for currentLog in currentLogs:
readLog = open(currentLog,"r")
for line in readLog.readlines():
if not line: continue
if len(line) < 50: continue
logLine = line.split()

# The 2nd element is month, 5th is year, 8th is filesize
# Counting from zero:

# Lookup year/month pair value
logMonth = logMonths[logLine[1]]
currentYearMonth = yearTotals[logLine[4]][logMonth]

# Update year/month value
currentYearMonth += int(logLine[7])
yearTotals[logLine[4]][logMonth] = currentYearMonth
except:
print "Failed on: " + currentLog
traceback.print_exc()

# Print dictionaries
for x in yearTotals.keys():
print "KEY",''\t'',"VALUE"
print x,''\t'',yearTotals[x]
#print " key",''\t'',"value"
for y in yearTotals[x].keys():
print " ",y,''\t'',yearTotals[x][y]
Thank you,
Ian

推荐答案

En Wed,2007年4月11日15:57:56 -0300,IamIan< ia **** @gmail.comescribió:
En Wed, 11 Apr 2007 15:57:56 -0300, IamIan <ia****@gmail.comescribió:

我正在编写一个简单的FTP日志解析器,它在运行时对文件大小求和。我

有一个yearTotals字典,年份密钥和monthTotals

字典作为其值。 monthTotals字典有月份键

和文件大小值。该脚本的工作原理除外,所有年份的结果都写成了b $ b,而不是仅仅一年。我在想我设置词典或引用它们的方式有一个

错误...
I''m writing a simple FTP log parser that sums file sizes as it runs. I
have a yearTotals dictionary with year keys and the monthTotals
dictionary as its values. The monthTotals dictionary has month keys
and file size values. The script works except the results are written
for all years, rather than just one year. I''m thinking there''s an
error in the way I set my dictionaries up or reference them...


monthTotals = dict.fromkeys(月,0)

#在yearTotals字典中嵌套monthTotals字典

yearTotals = {}

年份多年:

yearTotals.setdefault(年,月总计)
monthTotals = dict.fromkeys(months, 0)
# Nest monthTotals dictionary in yearTotals dictionary
yearTotals = {}
for year in years:
yearTotals.setdefault(year, monthTotals)



所有年份共享*相同* monthTotals对象。

这与此FAQ条目类似:

< http://effbot.org/pyfaq/how-do-i-create-a-multidimensional-list.htm>

你必须为每年创建一个新的dict;将以上代码替换为:


yearTotals = {}

年份:

yearTotals [year] = dict。 fromkeys(月,0)


-

Gabriel Genellina

All your years share the *same* monthTotals object.
This is similar to this FAQ entry:
<http://effbot.org/pyfaq/how-do-i-create-a-multidimensional-list.htm>
You have to create a new dict for each year; replace the above code with:

yearTotals = {}
for year in years:
yearTotals[year] = dict.fromkeys(months, 0)

--
Gabriel Genellina




IamIan < ia **** @ gmail.com写信息

news:11 ********************* @ n59g2000hsh.googlegro ups .com ...

|您好,

|

|我正在编写一个简单的FTP日志解析器,它在运行时对文件大小求和。我

|有一年yearTotals字典与年份密钥和monthTotals

|字典作为其价值观。 monthTotals字典有月份键

|和文件大小值。该脚本有效,除了结果写成

|所有年份,而不仅仅是一年。我在想有一个

|我设置词典或引用它们的方式错误...

|

| import glob,traceback

|

|年= [" 2005"," 2006"," 2007"]

|月= [" 01,02,03,04,05,06,07,08,09, 10,11,12]

| #创建月份字典以转换日志值

| logMonths =

|

{" Jan":" 01"," Feb":" 02"," Mar":" 03", "四月":" 04","五月" :05,Jun:06,Jul:07,Aug:08,Sep:09,Oct,Oct:J。 :10,Nov:11,Dec:12}

| #创建默认值为0的monthTotals字典

| monthTotals = dict.fromkeys(月,0)

| #在yearTotals字典中嵌套monthTotals字典

| yearTotals = {}

|多年的年份:

| yearTotals.setdefault(year,monthTotals)


尝试yearTotals.setdefault(年,dict.fromkeys(月,0))

所以你从单独开始每年一次,而不是一年一次。


tjr


"IamIan" <ia****@gmail.comwrote in message
news:11*********************@n59g2000hsh.googlegro ups.com...
| Hello,
|
| I''m writing a simple FTP log parser that sums file sizes as it runs. I
| have a yearTotals dictionary with year keys and the monthTotals
| dictionary as its values. The monthTotals dictionary has month keys
| and file size values. The script works except the results are written
| for all years, rather than just one year. I''m thinking there''s an
| error in the way I set my dictionaries up or reference them...
|
| import glob, traceback
|
| years = ["2005", "2006", "2007"]
| months = ["01","02","03","04","05","06","07","08","09","10", "11","12"]
| # Create months dictionary to convert log values
| logMonths =
|
{"Jan":"01","Feb":"02","Mar":"03","Apr":"04","May" :"05","Jun":"06","Jul":"07","Aug":"08","Sep":"09", "Oct":"10","Nov":"11","Dec":"12"}
| # Create monthTotals dictionary with default 0 value
| monthTotals = dict.fromkeys(months, 0)
| # Nest monthTotals dictionary in yearTotals dictionary
| yearTotals = {}
| for year in years:
| yearTotals.setdefault(year, monthTotals)

try yearTotals.setdefault(year, dict.fromkeys(months, 0))
so you start with a separate subdict for each year instead of 1 for all.

tjr


IamIanaécrit:
IamIan a écrit :

您好,


我正在编写一个简单的FTP日志解析器,它在运行时对文件大小求和。我

有一个yearTotals字典,年份密钥和monthTotals

字典作为其值。 monthTotals字典有月份键

和文件大小值。该脚本的工作原理除外,所有年份的结果都写成了b $ b,而不是仅仅一年。我在想我设置词典或引用它们的方式有一个

错误...


import glob,traceback


years = [" 2005"," 2006"," 2007"]

months = [" 01"," 02", 03,04,05,06,07,08,09,10,11,12,

#创建月份字典以转换日志值

logMonths =

{" Jan":" 01"," Feb": " 02"," MAR":" 03","四月":" 04","五月" :05,Jun:06,Jul:07,Aug:08,Sep:09,Oct,Oct:J。 :10,Nov:11,Dec:12}
Hello,

I''m writing a simple FTP log parser that sums file sizes as it runs. I
have a yearTotals dictionary with year keys and the monthTotals
dictionary as its values. The monthTotals dictionary has month keys
and file size values. The script works except the results are written
for all years, rather than just one year. I''m thinking there''s an
error in the way I set my dictionaries up or reference them...

import glob, traceback

years = ["2005", "2006", "2007"]
months = ["01","02","03","04","05","06","07","08","09","10", "11","12"]
# Create months dictionary to convert log values
logMonths =
{"Jan":"01","Feb":"02","Mar":"03","Apr":"04","May" :"05","Jun":"06","Jul":"07","Aug":"08","Sep":"09", "Oct":"10","Nov":"11","Dec":"12"}



DRY违规警报!


logMonths = {

" Jan":" 01",

" Feb":" 02",

" Mar":" 03",

" Apr":" 04",

" May":" 05" ,

#etc

}


months = sorted(logMonths.values())

DRY violation alert !

logMonths = {
"Jan":"01",
"Feb":"02",
"Mar":"03",
"Apr":"04",
"May":"05",
#etc
}

months = sorted(logMonths.values())


#创建默认值为0的monthTotals字典

monthTotals = dict.fromkeys(months,0)

#在yearTotals字典中嵌套monthTotals字典

yearTotals = {}

年份:

yearTotals.setdefault(year,monthTotals)
# Create monthTotals dictionary with default 0 value
monthTotals = dict.fromkeys(months, 0)
# Nest monthTotals dictionary in yearTotals dictionary
yearTotals = {}
for year in years:
yearTotals.setdefault(year, monthTotals)



一种复杂的wr方式ite:

yearTotals = dict((年,月总计)年份)


如果没有进一步阅读,我可以告诉你这里有问题:

年度总数中的所有年份条目指向*相同的* monthTotal dict

实例。因此,在更新yearTotals [''2007'']时,您会看到所有年份反映的变化

。治愈很简单:忘记monthTotals

对象,并用这种方式定义你的yearTotals dict:


yearTotals = dict((年,dict.fromkeys(月) ,0))多年的年份)


注意:对于Python版本< 2.4.x,你需要一个列表comp而不是一个

生成器表达式,即:


yearTotals = dict([(年,dict.fromkeys(月) ,0))多年的年份])


HTH

A complicated way to write:
yearTotals = dict((year, monthTotals) for year in years)

And without even reading further, I can tell you have a problem here:
all ''year'' entry in yearTotals points to *the same* monthTotal dict
instance. So when updating yearTotals[''2007''], you see the change
reflected for all years. The cure is simple: forget the monthTotals
object, and define your yearTotals dict this way:

yearTotals = dict((year, dict.fromkeys(months, 0)) for year in years)

NB : for Python versions < 2.4.x, you need a list comp instead of a
generator expression, ie:

yearTotals = dict([(year, dict.fromkeys(months, 0)) for year in years])

HTH


这篇关于嵌套字典麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆