根据输入创建一个包含三个单词短语的字典
Creating a dictionary of three word phrases based on input
def splitSentence(sentence):
dictionarySentence = {}
setence_split = sentence.split()
three_word_list = [' '.join(setence_split[i:i+3]) for i in range(0, len(setence_split), 3)]
#grouped_words = [' '.join(words[i: i + 3]) for i in range(0, len(words), 3)]
for key,char in enumerate(three_word_list):
{
dictionaryTweets.update({char:1})
}
return dictionaryTweets
return three_word_list
splitSentence("My name is Allen. How are you?")
输出:
{'My name is': 1, 'Allen. How are': 1, 'you?': 1}
寻找的输出:
{'My name is': 1, 'is Allen. How': 1, 'How are you?': 1}
输出应该是一个字典,其键都是基于输入到函数中的句子的三个单词短语。我不是 100% 知道你如何确保这些短语是三个词长。有人可以帮忙吗?
- 函数名称和定义不同。
- 您不能有 2 个 return 语句。
dictionaryTweets
未定义。
下面的代码工作正常。
def splitTextToTriplet(sentence):
dictionaryTweets = {}
three_word_list = []
setence_split = sentence.split()
for i in range(0,len(setence_split)-1,2):
three_word_list.append(' '.join(setence_split[i:i+3]))
for key, char in enumerate(three_word_list):
dictionaryTweets.update({char: 1})
return dictionaryTweets, three_word_list
print(splitTextToTriplet("My name is Allen. How are you?"))
def splitSentence(sentence):
dictionarySentence = {}
setence_split = sentence.split()
three_word_list = [' '.join(setence_split[i:i+3]) for i in range(0, len(setence_split), 3)]
#grouped_words = [' '.join(words[i: i + 3]) for i in range(0, len(words), 3)]
for key,char in enumerate(three_word_list):
{
dictionaryTweets.update({char:1})
}
return dictionaryTweets
return three_word_list
splitSentence("My name is Allen. How are you?")
输出:
{'My name is': 1, 'Allen. How are': 1, 'you?': 1}
寻找的输出:
{'My name is': 1, 'is Allen. How': 1, 'How are you?': 1}
输出应该是一个字典,其键都是基于输入到函数中的句子的三个单词短语。我不是 100% 知道你如何确保这些短语是三个词长。有人可以帮忙吗?
- 函数名称和定义不同。
- 您不能有 2 个 return 语句。
dictionaryTweets
未定义。
下面的代码工作正常。
def splitTextToTriplet(sentence):
dictionaryTweets = {}
three_word_list = []
setence_split = sentence.split()
for i in range(0,len(setence_split)-1,2):
three_word_list.append(' '.join(setence_split[i:i+3]))
for key, char in enumerate(three_word_list):
dictionaryTweets.update({char: 1})
return dictionaryTweets, three_word_list
print(splitTextToTriplet("My name is Allen. How are you?"))