Re: [問題] 請問字典功能
※ 引述《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 www@xx.com.tw', 'From www@xx.com.tw', 'From jj@cc.com.tw']
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
→
02/09 11:38, , 1F
02/09 11:38, 1F
→
02/09 11:42, , 2F
02/09 11:42, 2F
→
02/09 12:12, , 3F
02/09 12:12, 3F
→
02/09 12:13, , 4F
02/09 12:13, 4F
→
02/09 13:29, , 5F
02/09 13:29, 5F
討論串 (同標題文章)
Python 近期熱門文章
PTT數位生活區 即時熱門文章