在《机器学习实战》第二章中,使用了KNN算法改进约会网站配对效果。本文为在实现过程中使用到的一些函数 基本用法的总结。
1. str.strip()方法
语法:str.strip([chars])
功能:返回移除字符串头尾指定字符后生成的新字符串
参数: chars 需要移除字符串头尾的指定字符(默认是空格)
官方文档描述: Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped
示例:
>>>comment_string = '#....... Section 3.2.1 Issue #32 .......'
>>>comment_string.strip('.#! ')
'Section 3.2.1 Issue #32'