python清除字符串里非字母字符的方法

yipeiwu_com6年前Python基础

本文实例讲述了python清除字符串里非字母字符的方法。分享给大家供大家参考。具体如下:

s = "hello world! how are you? 0" 
# Short version 
print filter(lambda c: c.isalpha(), s) 
# Faster version for long ASCII strings: 
id_tab = "".join(map(chr, xrange(256))) 
tostrip = "".join(c for c in id_tab if c.isalpha()) 
print s.translate(id_tab, tostrip) 
# Using regular expressions 
print re.sub("[^A-Za-z]", "", s)

希望本文所述对大家的Python程序设计有所帮助。

相关文章

python 使用pandas计算累积求和的方法

使用pandas下的cumsum函数 cumsum:计算轴向元素累积加和,返回由中间结果组成的数组.重点就是返回值是"由中间结果组成的数组" import numpy as np '...

win10下python3.5.2和tensorflow安装环境搭建教程

win10下python3.5.2和tensorflow安装环境搭建教程

在win10环境下搭建python3.5.2和tensorflow平台,供大家参考,具体内容如下 操作步骤如下: 1、官网(https://www.python.org/ )下...

pytorch 实现tensor与numpy数组转换

看代码,tensor转numpy: a = torch.ones(2,2) b = a.numpy() c=np.array(a) #也可以转numpy数组 print(type(a...

python里 super类的工作原理详解

super 的工作原理如下: def super(cls, inst): mro = inst.__class__.mro() return mro[mro.index(cl...

python+matplotlib实现鼠标移动三角形高亮及索引显示

python+matplotlib实现鼠标移动三角形高亮及索引显示

Trifinder事件实例 实例展示Trifinder对象对的使用。当鼠标移动到一个被分割的三角形上,这个三角形高亮显示,并且它的标签在图标题显示。 展示下演示结果: 完整代码:...