作者aquarianboy (我是没牌的营养师!)
看板Python
标题Re: [问题] 请问字典功能
时间Mon Feb 9 10:52:49 2009
※ 引述《drlo (imagine)》之铭言:
: import string
: for line in infile:
: words= string.split (line)
: if len(words)>=2 and words[0]=="From":
: sec=words[1]
: #construct a new dictionary
: d={}
: #add one to the count if the email address existed
: d[sec]=d.get(sec,0)+1
: #prints out the dictionary contents
: for key,values in d.items():
: print key,values
我只帮原来的程式码调整了一下位置跟缩排..
import string
infile = ['From
[email protected]', 'From
[email protected]', 'From
[email protected]']
d={}
for line in infile:
words= string.split (line)
if len(words)>=2 and words[0]=="From":
sec=words[1]
d[sec]=d.get(sec,0)+1
for key,values in d.items():
print key,values
试试看吧 :)
(应该有更易读的写法才是)
--
诚徵
PHP网站工程师,熟
MVC架构佳
http://blog.eddie.com.tw/
全职、兼职可,意者请敲 :)
http://www.eddie.com.tw/
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.132.128.37
1F:→ drlo:太感谢了,跑得出来,不过这个结果呈现只有最後累加数字, 02/09 11:38
2F:→ drlo:即使是相同位址,但还是会呈现目前累计的数字,该怎麽改写呢ꤠ 02/09 11:42
3F:→ aquarianboy:在第一个for里的最後一行print words[1], d[sec] 02/09 12:12
4F:→ aquarianboy:最後面的for就不用了 02/09 12:13
5F:→ drlo:感谢aquarianboy的解答,我闷了几天总算跑得出来了..:) 02/09 13:29