• 手机站
  • 收藏
聚培教育网合作机构 > 长春达内教育
长春达内教育
400-998-6158
长春达内是专业的IT培训中心,专为各位学员提供php,java等程序设计课程
长春达内教育

Python中字符串的处理技巧有哪些

python学习网

更新时间:2021-10-29 浏览:93
核心提示:今日楼主要跟朋友共享的文章内容是有关Python中字符串数组的解决方法有什么?在探索文本分析方式时却不知道如何下手,应该怎么办?

今日楼主要跟朋友共享的文章内容是有关Python中字符串数组的解决方法有什么?在探索文本分析方式时却不知道如何下手,应该怎么办?那麼能否借助这一字符串数组解决基础教程,来了解一下运用Python解决字符串数组的一些操作过程。当今,自然语言理解处置和文本分析是分析和采用的热门行业。这种方面包含各种各样主要的专业技能和定义,在深层次实际实践活动以前*须对他们有完全的了解,因此,务必了解一些基本上的字符串数组实际操作和解决方法。为了更好地协助老大姐更强的从业Python有关工作中我为我们注意了这篇简洁明了的Python字符串数组解决基础教程,期待为新手有一定的协助,下边来和我一起看一看吧!

1. 空格符脱离

空格符脱离是字符串数组处置的一种操作过程,能够应用lstrip()方式 (左)脱离流板空格符,应用rstrip()(右)方式 对跟随空格符开展脱离,及其应用strip()脱离流板和跟随空格符。

s = ' This is a sentence with whitespace. n'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')))

Strip unwanted characters: This is a sentence with unwanted characters.

必需时别忘记查验字符串数组 format()文本文档。

format()文本文档:https://docs.python.org/3/library/stdtypes.html#str.format

2. 字符串数组分拆

运用Python中的 split() 方式 能够随意将字符串数组拆分为较小的子字符串数组目录。

split() 方式 :https://docs.python.org/3/library/stdtypes.html#str.split

s = 'KDnuggets is a fantastic resource'print(s.split())

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

默认设置状况下,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']

3. 将目录元素合成字符串数组

*须 完成以上使用的一个反向实际操作?没什么问题,运用Python中的join()方式 便可将目录中的元素合成一个字符串数组。

join()方式 :https://docs.python.org/3/library/stdtypes.html#str.join

s = ['KDnuggets', 'is', 'a', 'fantastic', 'resource']print(' '.join(s))

KDnuggets is a fantastic resource

客观事实正是如此!假如想将目录原素用空格符之外的物品相互连接?这也许有点儿生疏,但也容易完成。

s = ['Eleven', 'Mike', 'Dustin', 'Lucas', 'Will']print(' and '.join(s))

Eleven and Mike and Dustin and Lucas and Will

4. 字符串反转

Python沒有内嵌的字符串反转方式 。可是,能够先将字符串数组看作是标识符的目录,再运用翻转目录原素的方法开展翻转。

5. 大小写转换

运用upper(), lower(),和swapcase()方式 需要开展英文大小写中间的变换。

upper()方式 :https://docs.python.org/3/library/stdtypes.html#str.upperlower()方式 :https://docs.python.org/3/library/stdtypes.html#str.lowerswapcase()方式 :https://docs.python.org/3/library/stdtypes.html#str.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

6. 查验是不是有字符串数组组员

在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。对这一默认设置状况把握禁止时,能够查看一下有关文本文档。

7. 子字符串替换

寻找子字符串数组以后,假如想更换这一子字符串数组,应该怎么办?Python 中的replace()字符串数组方式 将缓解这一难题。

replace()字符串数组方式 :https://docs.python.org/3/library/stdtypes.html#str.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.

假如同一个子字符串数组发生数次得话,运用记数主要参数这一选择项,能够规定要实现持续更换的*频次。

8. 组成好几个目录的輸出

怎样以某类因素的形式将好几个字符串数组目录组成在一起?运用zip()涵数便没什么问题。

zip()涵数:https://docs.python.org/3/library/functions.html#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.

9. 同英文字母异序词查验

想查验一对字符串数组中,在其中一个字符串数组是不是另一个字符串数组的同英文字母异序词?从优化算法上而言,*须 做的是对每一个字符串数组中各个符号的发生频次完成记数,再查验二者计标值是不是相同,立即应用collections控制模块的Counter类便可完成。

collections控制模块的Counter类:https://docs.python.org/3/library/collections.html#collections.Counter

from collections import Counterdef 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

10. 回文查验

假如想查验给出的英语单词是不是回文,该怎么办?从优化算法上看,*须 建设一个英语单词的翻转,随后运用 == 操作符来查验这两个字符串数组(初始字符串数组和反方向字符串数组)是不是相同。

def is_palindrome(s): reverse = s[::-1] if (s == reverse): return True return Falses1 = '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

尽管把握这种字符串数组解决“方法”以后,并不代表你早已变成了文本分析或自然语言理解解决**专家,但这种方法很有可能会激起出深层次研究自然语言理解处理方面的兴趣爱好,并把握最后形成**专家所准备的专业技能。

之上便是我们现在为大伙儿共享的有关Python中字符串数组的解决方法是什么的文章内容,期待这篇小文章可以对已经从业Python有关工作中和提升的小伙伴有一定的协助,要想知道大量Python有关专业知识还记得关心达内教育Python学习培训官方网站。最终祝福朋友们一切顺利,变成一名优异的Python技术工程师!

更多>同类资讯
更多>相关课程
顶部