문제

I am unsure how to complete this problem in python 3.x.x Define a function wordsD that takes one parameter: 1. text, a string text contains sentences with words separated by spaces and ending in periods. The function returns a dictionary of word/sentenceList pairs, where word is each distinct word in the text and sentenceList is the list of sentence indices the word appear in. Note: your code is insensitive to case lettering for example if text='I say what I mean. I mean what I say. i do." function returns {'i":[0,1,2],'say':[0,1],'what':[0,1],'mean':[0,1],'do':[2]}

도움이 되었습니까?

해결책 2

For some reason people often ask how to do this without 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])}

If you really want lists, here is one modification to do it

>>> 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]}

If you're not even allowed to use enumerate

>>> 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]}

다른 팁

You can use collections.defaultdict here:

>>> from collections import defaultdict
>>> text= "I say what I mean. I mean what I say. i do."
#  convert the text to lower-case and split at `'.'` to get the sentences.
>>> sentences = text.lower().split('.')  
>>> dic = defaultdict(set)       #sets contain only unique iteme
for i,sen in enumerate(sentences): #use enumerate to get the sentence as well as index
    for word in sen.split():       #split the sentence at white-spaces to get words
        dic[word].add(i)

>>> dic
defaultdict(<type 'set'>,
{'i': set([0, 1, 2]),
 'do': set([2]),
 'say': set([0, 1]),
 'what': set([0, 1]),
 'mean': set([0, 1])})

Using a normal dict:

>>> dic = {}
for i,sen in enumerate(sentences):
    for word in sen.split():
        dic.setdefault(word,set()).add(i)
...         
>>> dic
{'i': set([0, 1, 2]),
 'do': set([2]),
 'say': set([0, 1]),
 'what': set([0, 1]),
 'mean': set([0, 1])}

Without enumerate:

>>> dic = {}
>>> index = 0
for sen in sentences:
    for word in sen.split():
        dic.setdefault(word,set()).add(index)
    index += 1
...     
>>> dic
{'i': set([0, 1, 2]), 'do': set([2]), 'say': set([0, 1]), 'what': set([0, 1]), 'mean': set([0, 1])}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top