python两列字符串文本匹配_青灯教育|Python字符串处理,文本分析技巧

1b076708f36b0f69b6a240e7a36a634d.png

在当前自然语言处理和文本分析是研究和应用的热点领域。而该领域包含各种具体的技能和概念,在深入实践之前需要对它们有彻底的理解,为此必须掌握一些基本的字符串操作和处理技巧。

在这里我们主要讲解“利用给定编程语言的标准库进行基本的字符串操作。”当然实际意义的文本分析将远远超出字符串处理的范畴,而那些更加先进的核心技术可能并不需要你频繁的亲自对文本进行操作,然而文本数据预处理对于一个成功的文本分析项目来说,是至关重要和耗费时间的环节。这时候文本涵盖的字符串处理技能就显得很有价值了。

空格剥离

空格剥离作为处理字符串的基本操作,常用方法有lstrip()(剥离签到空格)、rstrip()(剥离尾随空格)、strip()(剥离前导和尾随空格)。

s = ' This is a sentence with whitespace. '

print('Strip leading whitespace: {}'.format(s.lstrip()))

print('Strip trailing whitespace: {}'.format(s.rstrip()))

print('Strip all whitespace: {}'.format(s.strip()))

Strip leading whitespace: This is a sentence with whitespace.

Strip trailing whitespace: This is a sentence with whitespace.

Strip all whitespace: This is a sentence with whitespace.

当然同样的方法也有很多,另一个比较常见的就是通过指定想要剥离的字符来处理字符串:

s = 'This is a sentence with unwanted characters.AAAAAAAA'

print('Strip unwanted characters: {}'.format(s.rstrip('A')))

字符串拆分

字符串拆分是利用Python中的split()将字符串拆分成较小的字符串列表。

s = 'KDnuggets is a fantastic resource'

print(s.split())

未加参数时,split()默认根据空格进行拆分,但同样也可以按指定字符进行拆分字符串。

s = 'these,words,are,separated,by,comma'

print('',' separated split -> {}'.format(s.split(',')))

s = 'abacbdebfgbhhgbabddba'

print(''b' separated split -> {}'.format(s.split('b')))

',' separated split -> ['these', 'words', 'are', 'separated', 'by', 'comma']

'b' separated split -> ['a', 'ac', 'de', 'fg', 'hhg', 'a', 'dd', 'a']

将列表元素合成字符串

上述讲了如何讲一个字符串拆分成许多了,这里讲如何将许多个字符串合成一个字符串。那就要用到join()方法。

s = ['KDnuggets', 'is', 'a', 'fantastic', 'resource']

print(' '.join(s))

KDnuggets is a fantastic resource

字符串反转

Python目前没有字符串反转的方法,但是我们可以先将一个字符串当做多个字符组成的列表,在利用反转表元素的方式对整个字符串进行反转。

大小写转换

Python中字符串的大小写转换还是非常简单的,只需要利用好upper()、lower()、swapcase()这三个方法,就能实现大小写之间的转换。

s = 'KDnuggets'

print(''KDnuggets' as uppercase: {}'.format(s.upper()))

print(''KDnuggets' as lowercase: {}'.format(s.lower()))

print(''KDnuggets' as swapped case: {}'.format(s.swapcase()))

'KDnuggets' as uppercase: KDNUGGETS

'KDnuggets' as lowercase: kdnuggets

'KDnuggets' as swapped case: kdNUGGETS

检查是否有字符串成员

Python中检测字符串成员最简单的方法就是使用in运算符。它的语法和自然语十分相似。

s1 = 'perpendicular'

s2 = 'pen'

s3 = 'pep'

print(''pen' in 'perpendicular' -> {}'.format(s2 in s1))

print(''pep' in 'perpendicular' -> {}'.format(s3 in s1))

'pen' in 'perpendicular' -> True

'pep' in 'perpendicular' -> False

当然如果不单单只是为了检测字符是否存在,而是要找到具体的位置,则需要使用find()方法。

s = 'Does this string contain a substring?'

print(''string' location -> {}'.format(s.find('string')))

print(''spring' location -> {}'.format(s.find('spring')))

'string' location -> 10

'spring' location -> -1

默认情况下,find()返回子字符串第一次出现的第一个字符的索引,如果找不到子字符串,则返回-1。

子字符串替换

如果在找到字符串之后,我们想替换这一字符串,该怎么办?那就要用到replace()方法的功能。

s1 = 'The theory of data science is of the utmost importance.'

s2 = 'practice'

print('The new sentence: {}'.format(s1.replace('theory', s2)))

The new sentence: The practice of data science is of the utmost importance.

如果同一个子字符串出现多次的话,利用计数参数这一选项,可以指定要进行连续替换的最大次数。

组合多个列表的输出

将多了字符串列表组合在一起,需要利用到zip()方法。

countries = ['USA', 'Canada', 'UK', 'Australia']

cities = ['Washington', 'Ottawa', 'London', 'Canberra']

for x, y in zip(countries, cities):

print('The capital of {} is {}.'.format(x, y))

The capital of USA is Washington.

The capital of Canada is Ottawa.

The capital of UK is London.

The capital of Australia is Canberra.

同字母异序词检查

想检查一对字符串中,其中一个字符串是否是另一个字符串的同字母异序词?从算法上来讲,需要做的是对每个字符串中每个字母的出现次数进行计数,再检查二者计数值是否相等,直接使用collections模块的Counter类便可实现。

from collections import Counter

def is_anagram(s1, s2):

return Counter(s1) == Counter(s2)

s1 = 'listen'

s2 = 'silent'

s3 = 'runner'

s4 = 'neuron'

print(''listen' is an anagram of 'silent' -> {}'.format(is_anagram(s1, s2)))

print(''runner' is an anagram of 'neuron' -> {}'.format(is_anagram(s3, s4)))

'listen' an anagram of 'silent' -> True

'runner' an anagram of 'neuron' -> False

回文检查

如果想检查给定的单词是否是回文,怎么办?从算法上看,需要创建一个单词的反转,然后利用 == 运算符来检查这2个字符串(原始字符串和反向字符串)是否相等。

def is_palindrome(s):

reverse = s[::-1]

if (s == reverse):

return True

return False

s1 = 'racecar'

s2 = 'hippopotamus'

print(''racecar' a palindrome -> {}'.format(is_palindrome(s1)))

print(''hippopotamus' a palindrome -> {}'.format(is_palindrome(s2)))

'racecar' is a palindrome -> True

'hippopotamus' is a palindrome -> False

虽然掌握这些字符串处理“技巧”之后,并不意味着你已经成为了文本分析或自然语言处理专家,但这些技巧可能会激发出深入探究自然语言处理领域的兴趣,并掌握最终成为专家所必备的技能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值