计算字符串中的单词数量?

时间:2016-08-01 16:02:08

标签: python

我写过这个函数。

# Function to count words in a string.
def word_count(string):
    tokens = string.split()
    n_tokens = len(tokens)
    print (n_tokens)

# Test the code.
print(word_count("Hello World!"))
print(word_count("The quick brown fox jumped over the lazy dog."))

但输出是

2
None
9 
None

而不仅仅是

2
9

2 个答案:

答案 0 :(得分:1)

var checkforoneorffith = function(number){ return (number - 1) % 5 == 0; } 没有word_count语句,因此隐式返回return。您的函数会打印令牌数None,然后您的函数调用print (n_tokens)会打印print(word_count("Hello World!"))

答案 1 :(得分:0)

除了布莱恩所说的,这段代码说明了如何得到你想要的东西:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  // use indexPath.section and indexPath.row to use your data
  let infoVC = yourInfoViewController()
  infoVC.title = "Info Cell Name"
  navigationController?.pushViewController(infoVC, animated: true)
}
相关问题