【自然语言处理(一)】--相关基础技能

1.python字符串相关操作

s1 = " hello "
s2 = " world! "
#去除字符串左右两边的空格
s1 = s1.strip()
s2 = s2.strip()
#拼接字符串
s = s1+s2
#查找字符或子串 
s_index = s.index('hello')
#字符串大小写转换
s3 = "ABC"
s4 = "abc"
s3_lower = s3.lower()
s4_upper = s4.upper()
#翻转字符串
s_reverse = s[::-1]
#查找字符串
s_find = s.find("hello")
#分割字符串
s5 = "a,b,c,d,e"
s5_split = s5.split(",")
import re
from collections import Counter
#获取字符串中出现最多次数的字符
def count_char(str):
    str = str.lower()
    res = re.findall("[a-z]",str)
    count = Counter(res)
    return [k for k,v in count.items() if v==max(list(count.values()))]

2.正则表达式(网上很多教程,关键还是理解每一个代表什么意思,还要多写,其实没什么大不了,这里就不写了)就只写写python中是怎么用的

猜你喜欢

转载自www.cnblogs.com/xiximayou/p/11829706.html
今日推荐