使用字典和字符串的单词/对列表

我不确定如何在python 3.xx中完成此问题,请定义一个带一个参数的words函数:1.文本,字符串文本包含带有单词的句子,单词之间用空格分隔并以句点结尾。该函数返回一个word / sentenceList对的字典,其中word是文本中的每个不同单词,而句子列表是该单词出现的句子索引列表。注意:例如,如果text ='I say,您的代码对大小写不敏感。我的意思是。我的意思是我说的。我做。”函数返回{'i“:[0,1,2],'say':[0,1],'what':[0,1],'mean':[0,1],'要做:[2]}


翻翻过去那场雪
浏览 156回答 2
2回答

HUH函数

由于某些原因,人们经常问如何在没有defaultdict的情况下执行此操作>>> text= "I say what I mean. I mean what I say. i do.">>> sentences = text.lower().split('.')>>> dic = {}>>> for i, sen in enumerate(sentences):...     for word in sen.split():...         if word not in dic:         # you just need these...             dic[word] = set()       # two extra lines...         dic[word].add(i)... >>> dic{'i': set([0, 1, 2]), 'do': set([2]), 'say': set([0, 1]), 'what': set([0, 1]), 'mean': set([0, 1])}如果您确实想要列表,可以通过以下修改来做到这一点>>> text= "I say what I mean. I mean what I say. i do.">>> sentences = text.lower().split('.')>>> dic = {}>>> for i, sen in enumerate(sentences):...     for word in sen.split():...         if word not in dic:...             dic[word] = [i]...         elif dic[word][-1] != i:     # this prevents duplicate entries...             dic[word].append(i)... >>> dic{'i': [0, 1, 2], 'do': [2], 'say': [0, 1], 'what': [0, 1], 'mean': [0, 1]}如果您甚至不被允许使用枚举>>> text= "I say what I mean. I mean what I say. i do.">>> sentences = text.lower().split('.')>>> dic = {}>>> i = -1>>> for sen in sentences:...     i += 1...     for word in sen.split():...         if word not in dic:...             dic[word] = [i]...         elif dic[word][-1] != i:     # this prevents duplicate entries...             dic[word].append(i)... >>> dic{'i': [0, 1, 2], 'do': [2], 'say': [0, 1], 'what': [0, 1], 'mean': [0, 1]}

暮色呼如

您可以collections.defaultdict在这里使用:>>> from collections import defaultdict>>> text= "I say what I mean. I mean what I say. i do."#&nbsp; convert the text to lower-case and split at `'.'` to get the sentences.>>> sentences = text.lower().split('.')&nbsp;&nbsp;>>> dic = defaultdict(set)&nbsp; &nbsp; &nbsp; &nbsp;#sets contain only unique itemefor i,sen in enumerate(sentences): #use enumerate to get the sentence as well as index&nbsp; &nbsp; for word in sen.split():&nbsp; &nbsp; &nbsp; &nbsp;#split the sentence at white-spaces to get words&nbsp; &nbsp; &nbsp; &nbsp; dic[word].add(i)>>> dicdefaultdict(<type 'set'>,{'i': set([0, 1, 2]),&nbsp;'do': set([2]),&nbsp;'say': set([0, 1]),&nbsp;'what': set([0, 1]),&nbsp;'mean': set([0, 1])})使用普通字典:>>> dic = {}for i,sen in enumerate(sentences):&nbsp; &nbsp; for word in sen.split():&nbsp; &nbsp; &nbsp; &nbsp; dic.setdefault(word,set()).add(i)...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;>>> dic{'i': set([0, 1, 2]),&nbsp;'do': set([2]),&nbsp;'say': set([0, 1]),&nbsp;'what': set([0, 1]),&nbsp;'mean': set([0, 1])}没有enumerate:>>> dic = {}>>> index = 0for sen in sentences:&nbsp; &nbsp; for word in sen.split():&nbsp; &nbsp; &nbsp; &nbsp; dic.setdefault(word,set()).add(index)&nbsp; &nbsp; index += 1...&nbsp; &nbsp; &nbsp;>>> dic{'i': set([0, 1, 2]), 'do': set([2]), 'say': set([0, 1]), 'what': set([0, 1]), 'mean': set([0, 1])}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python