前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >10个Python字符串处理技巧和窍门(2)

10个Python字符串处理技巧和窍门(2)

作者头像
计算机与AI
发布2020-12-14 15:24:01
5000
发布2020-12-14 15:24:01
举报
文章被收录于专栏:计算机与AI计算机与AI

昨天的文章中,我们讲到了前五种技巧,今天我们继续了解后五种技巧!

6.检查字符串成员资格

在Python中检查字符串成员资格的最简单方法是使用in运算符。语法非常自然。

代码语言:javascript
复制
s1 = 'perpendicular'
s2 = 'pen'
s3 = 'pep'

print('\'pen\' in \'perpendicular\' -> {}'.format(s2 in s1))
print('\'pep\' in \'perpendicular\' -> {}'.format(s3 in s1))
代码语言:javascript
复制
'pen' in 'perpendicular' -> True
'pep' in 'perpendicular' -> False

如果您对查找字符串中子字符串的位置更感兴趣(而不是简单地检查是否包含子字符串),那么find()字符串方法可能会更有帮助。

代码语言:javascript
复制
s = 'Does this string contain a substring?'

print('\'string\' location -> {}'.format(s.find('string')))
print('\'spring\' location -> {}'.format(s.find('spring')))
代码语言:javascript
复制
'string' location -> 10
'spring' location -> -1

find()默认情况下,返回子字符串首次出现的第一个字符的索引,如果未找到子字符串,则返回-1。请查阅文档,以了解对此默认行为的可用调整。

7.替换子字符串

如果要替换子字符串而不是仅仅找到它们怎么办?Python replace()字符串方法将解决这一问题。

代码语言:javascript
复制
s1 = 'The theory of data science is of the utmost importance.'
s2 = 'practice'

print('The new sentence: {}'.format(s1.replace('theory', s2)))
代码语言:javascript
复制
The new sentence: The practice of data science is of the utmost importance.

可选的count参数可以指定如果同一子字符串多次出现,则要进行的最大连续替换数。

8.合并多个列表的输出

您是否有多个要以某种元素方式组合在一起的字符串列表?zip()功能没问题。

代码语言:javascript
复制
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))
代码语言:javascript
复制
The capital of USA is Washington.
The capital of Canada is Ottawa.
The capital of UK is London.
The capital of Australia is Canberra.

9.检查字谜

是否想检查一对字符串是否彼此相似?从算法上讲,我们要做的就是计算每个字符串中每个字母的出现次数,并检查这些计数是否相等。使用模块的Countercollections很简单。

代码语言:javascript
复制
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)))
代码语言:javascript
复制
'listen' an anagram of 'silent' -> True
'runner' an anagram of 'neuron' -> False

10.检查回文

如果要检查给定的单词是否是回文,该怎么办?从算法上讲,我们需要创建单词的反向,然后使用==运算符检查这两个字符串(原始字符串和反向字符串)是否相等。

代码语言:javascript
复制
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)))
代码语言:javascript
复制
'racecar' is a palindrome -> True
'hippopotamus' is a palindrome -> False

这些字符串处理“技巧”并不能使您自己成为文本分析或自然语言处理专家,但它们可能会使某人对追求这些领域和学习最终成为这种专家所必需的技术感兴趣。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-08-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 计算机与AI 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 6.检查字符串成员资格
  • 7.替换子字符串
  • 8.合并多个列表的输出
  • 9.检查字谜
  • 10.检查回文
相关产品与服务
NLP 服务
NLP 服务(Natural Language Process,NLP)深度整合了腾讯内部的 NLP 技术,提供多项智能文本处理和文本生成能力,包括词法分析、相似词召回、词相似度、句子相似度、文本润色、句子纠错、文本补全、句子生成等。满足各行业的文本智能需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档