成语大全网 - 成语词典 - 急!一道关于python字典的编程题!求思路!

急!一道关于python字典的编程题!求思路!

帮你实现了 直接上代码吧:

#! /usr/bin/env python

#coding=utf-8

#author qihigh

class AnimalStation:

__station1 = None

__station2 = None

def __init__(self):

self.__station1 = 0

self.__station2 = 0

def toStat(self,event):

if(event == 's1'):

self.__station1 = self.__station1+1

else:

self.__station2 = self.__station2+1

def result(self):

return [self.__station1,self.__station2]

if __name__ == '__main__':

str = ['''-----------此处省略700字,你懂得---------- '''']

#注: a01 -- a09 处理为 a1 -- a9

dic = {}

for i in range(1,50):

#a0 = AnimalStation()

exec("a"+`i`+" = AnimalStation()")

#dic['a0'] = a0

exec("dic['a"+`i`+"'] = a"+`i`)

for eachLine in str:

strs = eachLine.split(':')

animal,event = strs[0],strs[2]

#dic['a28'].to('s1')

exec("dic['"+animal+"'].toStat('"+event+"')")

print("Animail\t\tStation1\t\tStation2")

for i in range(1,50):

#print dic['a0'].result()

exec("result = dic['a"+`i`+"'].result()")

print("a"+`i`+"\t\t"+`result[0]`+"\t\t"+`result[1]`)

原理:利用的python的动态特性。

执行结果:

Animail Station1 Station2

a1 0 0

a2 0 1

a3 0 0

a4 1 0

a5 0 0

a6 1 0

a7 1 0

a8 1 1

a9 0 0

a10 2 0

a11 2 0

a12 0 0

a13 0 0

a14 1 0

a15 0 0

a16 0 0

a17 0 0

a18 0 2

a19 1 0

a20 0 0

a21 0 0

a22 0 1

a23 0 0

a24 1 0

a25 0 0

a26 0 0

a27 0 0

a28 1 0

a29 0 0

a30 0 0

a31 1 1

a32 0 0

a33 0 1

a34 0 1

a35 0 1

a36 2 0

a37 2 0

a38 1 0

a39 0 1

a40 0 0

a41 1 0

a42 0 1

a43 0 0

a44 0 0

a45 0 0

a46 0 1

a47 0 0

a48 0 0

a49 0 1

你想要的结果!!!分给我呗。。。。。。。