用Python循环单词列表

用Python循环单词列表,python,string,list,Python,String,List,我有一个函数,它将字典中的一个单词和一个句子(sent)连接起来。 问题是在这个循环中: for word in s: 它不是每个单词(句子中的单词连用),而是每个字符 我不知道为什么 有什么想法吗 谢谢 def compute_tfidf(word_counter, sent): word_counter = {} total_count = 0 no_of_sentences = 0 sents_emd = [] print(sent)

我有一个函数,它将字典中的一个单词和一个句子(sent)连接起来。 问题是在这个循环中:

  for word in s:
它不是每个单词(句子中的单词连用),而是每个字符

我不知道为什么

有什么想法吗

谢谢

def compute_tfidf(word_counter, sent):
    word_counter = {}
    total_count = 0
    no_of_sentences = 0    
    sents_emd = []
    print(sent)

    for s in sent :
        total_count = total_count + len(s)

        no_of_sentences = no_of_sentences +  1



        for word in s:
            print(word)
            print(word_counter[word])

    return sents_emd

如果
s
是一个字符串,您将逐个字符循环它。如果要循环句子字符串中的每个单词,请尝试对s.split()中的单词执行
,默认情况下,这将把空格中的字符串拆分为列表


如果
total\u count
应该对单词进行计数,那么您可能也希望使用
len(s.split())
。。在这一点上,您可能只想在循环的开头使用一个变量,如
语句=s.split()

您可能正在寻找
字符串中的单词。split(“”)