脚本练习汇总

练习1:

test.txt中不同元素由空格分开,写脚本允许用户输入某元素,如果该元素在文本中,输出元素个数;否则输出not found

答案: 

d = {}
for i in open('test.txt','r').read().split():
          d[i] = d.get(i,0) + 1
k = raw_input('please input string you want :')
print d.get(k,'Not Found')

 

posted @ 2013-10-29 10:07  brickisku  阅读(206)  评论(14编辑  收藏  举报