一道python的题目,求大神解答

fo.txtthisisthefirstlinethisisthesecondlinethisisthethirdlinethisistheforthlinethisis... fo.txt

this is the first line
this is the second line
this is the third line
this is the forth line

this is the fifth line
this is the sixth line
this is the seventh line

/////////

///////////我的代码:
def invertWordDict(fileName):
inputFile = open(fileName, 'r')

list1 = [ ]
dict1 = { }
dict2=[]

for line in inputFile:

line=line.strip('\n')
line=line.lower()
list1.append(line)
if ' ' in list1:
list1.remove('')
list1 = [word for line in list1 for word in line.split('!')]
list1 = [word for line in list1 for word in line.split('?')]
list1 = [word for line in list1 for word in line.split('.')]
list1 = [word for line in list1 for word in line.split()]
flag=1
for word in list1:
if word in dict1:
dict1[word] += 1

else:
dict1[word] = 1

return dict1

我的代码是输出 {‘this’:7 , 'is': 7, 'the':7 ,'first':1, 'second':1, 'third':1, 'forth':1,'fifth':1 ,'sixth':1, 'seventh':1, 'line':3}
(就是把每一个单词都编入字典,然后字典里显示每一个单词的个数)
但是如何在字典里每一项倒过来,并且归在一起,来实现:
{7:[‘this’,'is','the'],1:['first','second','third','forth','fifth','sixth','seventh'],3['line']}

要怎样继续编下去,能提供解决的思路和代码吗
展开
 我来答
mieyoubeizhuce
2015-10-22 · TA获得超过610个赞
知道小有建树答主
回答量:733
采纳率:100%
帮助的人:459万
展开全部

追问
万分感谢^ _ ^
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式