网上看到一个面试题,描述如下:
有一个文件file1,文件内容如下:
Hello world
Hello perl
hello world
Goodmorning
good news
123
请用perl,python或者java写一个程序,计算file1中首字母大写的单词出现的次数。
python实现代码如下:
f=open('test','r+')lines=f.readlines()
list=[]
a={}
for line in lines:
if line[0].isupper():
first_word = line.split(' ')[0]
list.append(first_word)
for i in list:
a[i]=list.count(i)
print a