python3-cookbook中一些关于字符串和文本的处理方式
2018-03-23 13:27
691 查看
1.查找最大或最小的 N 个元素
heapq 模块有两个函数:nlargest() 和 nsmallest() 可以完美解决这个问题。import heapq nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2] nbig = heapq.nlargest(3, nums) nsmall = heapq.nsmallest(3, nums)
两个函数都能接受一个关键字参数,用于更复杂的数据结构中:
portfolio = [ {'name': 'IBM', 'shares': 100, 'price': 91.1}, {'name': 'AAPL', 'shares': 50, 'price': 543.22}, {'name': 'FB', 'shares': 200, 'price': 21.09}, {'name': 'HPQ', 'shares': 35, 'price': 31.75}, {'name': 'YHOO', 'shares': 45, 'price': 16.35}, {'name': 'ACME', 'shares': 75, 'price': 115.65} ] cheap = heapq.nsmallest(3, portfolio, key=lambda s: s['price']) expensive = heapq.nlargest(3, portfolio, key=lambda s: s['price'])
注:上面代码在对每个元素进行对比的时候,会以 price 的值进行比较。
当要查找的元素个数相对比较小的时候,函数 nlargest() 和 nsmallest() 是很合适的。 如果你仅仅想查找唯一的最小或最大(N=1)的元素的话,那么使用 min() 和 max() 函数会更快些。 类似的,如果 N 的大小和集合大小接近的时候,通常先排序这个集合然后再使用切片操作会更快点 ( sorted(items)[:N] 或者是 sorted(items)[-N:] )。 需要在正确场合使用函数 nlargest() 和 nsmallest() 才能发挥它们的优势 (如果 N 快接近集合大小了,那么使用排序操作会更好些)。
2.怎样在数据字典中执行一些计算操作(比如求最小值、最大值、排序等等)?
考虑下面的股票名和价格映射字典:prices = { 'ACME': 45.23, 'AAPL': 612.78, 'IBM': 205.55, 'HPQ': 37.20, 'FB': 10.75 }
为了对字典值执行计算操作,通常需要使用 zip() 函数先将键和值反转过来。 比如,下面是查找最小和最大股票价格和股票值的代码:
min_price = min(zip(prices.values(), prices.keys())) # min_price is (10.75, 'FB') max_price = max(zip(prices.values(), prices.keys())) # max_price is (612.78, 'AAPL') 类似的,可以使用 zip() 和 sorted() 函数来排列字典数据: prices_sorted = sorted(zip(prices.values(), prices.keys())) # prices_sorted is [(10.75, 'FB'), (37.2, 'HPQ'), # (45.23, 'ACME'), (205.55, 'IBM'), # (612.78, 'AAPL')]
执行这些计算的时候,需要注意的是 zip() 函数创建的是一个只能访问一次的迭代器。 比如,下面的代码就会产生错误:
prices_and_names = zip(prices.values(), prices.keys()) print(min(prices_and_names)) # OK print(max(prices_and_names)) # ValueError: max() arg is an empty sequence
3.怎样在两个字典中寻寻找相同点(比如相同的键、相同的值等等)?
考虑下面两个字典:a = { 'x' : 1, 'y' : 2, 'z' : 3 } b = { 'w' : 10, 'x' : 11, 'y' : 2 }
为了寻找两个字典的相同点,可以简单的在两字典的 keys() 或者 items() 方法返回结果上执行集合操作。比如:
# Find keys in common a.keys() & b.keys() # { 'x', 'y' } # Find keys in a that are not in b a.keys() - b.keys() # { 'z' } # Find (key,value) pairs in common a.items() & b.items() # { ('y', 2) }
这些操作也可以用于修改或者过滤字典元素。 比如,假如你想以现有字典构造一个排除几个指定键的新字典。 下面利用字典推导来实现这样的需求:
# Make a new dictionary with certain keys removed c = {key:a[key] for key in a.keys() - {'z', 'w'}} # c is {'x': 1, 'y': 2}
4.你有一个字典列表,你想根据某个或某几个字典字段来排序这个列表。
通过使用 operator 模块的 itemgetter 函数,可以非常容易的排序这样的数据结构。 假设你从数据库中检索出来网站会员信息列表,并且以下列的数据结构返回:rows = [ {'fname': 'Brian', 'lname': 'Jones', 'uid': 1003}, {'fname': 'David', 'lname': 'Beazley', 'uid': 1002}, {'fname': 'John', 'lname': 'Cleese', 'uid': 1001}, {'fname': 'Big', 'lname': 'Jones', 'uid': 1004} ]
根据任意的字典字段来排序输入结果行是很容易实现的,代码示例:
from operator import itemgetter rows_by_fname = sorted(rows, key=itemgetter('fname')) rows_by_uid = sorted(rows, key=itemgetter('uid')) print(rows_by_fname) print(rows_by_uid)
最后,不要忘了这节中展示的技术也同样适用于 min() 和 max() 等函数。比如:
>>> min(rows, key=itemgetter('uid')) {'fname': 'John', 'lname': 'Cleese', 'uid': 1001} >>> max(rows, key=itemgetter('uid')) {'fname': 'Big', 'lname': 'Jones', 'uid': 1004} >>>
5.你需要将一个字符串分割为多个字段,但是分隔符(还有周围的空格)并不是固定的。
string 对象的 split() 方法只适应于非常简单的字符串分割情形, 它并不允许有多个分隔符或者是分隔符周围不确定的空格。 当你需要更加灵活的切割字符串的时候,最好使用 re.split() 方法:>>> line = 'asdf fjdk; afed, fjek,asdf, foo' >>> import re >>> re.split(r'[;,\s]\s*', line) ['asdf', 'fjdk', 'afed', 'fjek', 'asdf', 'foo']
函数 re.split() 是非常实用的,因为它允许你为分隔符指定多个正则模式。 比如,在上面的例子中,分隔符可以是逗号,分号或者是空格,并且后面紧跟着任意个的空格。 只要这个模式被找到,那么匹配的分隔符两边的实体都会被当成是结果中的元素返回。 返回结果为一个字段列表,这个跟 str.split() 返回值类型是一样的。
6.你想在字符串中搜索和匹配指定的文本模式
对于简单的字面模式,直接使用 str.replace() 方法即可,比如:>>> text = 'yeah, but no, but yeah, but no, but yeah' >>> text.replace('yeah', 'yep') 'yep, but no, but yep, but no, but yep' >>>
对于复杂的模式,请使用 re 模块中的 sub() 函数。 为了说明这个,假设你想将形式为 11/27/2012 的日期字符串改成 2012-11-27 。示例如下:
>>> text = 'Today is 11/27/2012. PyCon starts 3/13/2013.' >>> import re >>> re.sub(r'(\d+)/(\d+)/(\d+)', r'\3-\1-\2', text) 'Today is 2012-11-27. PyCon starts 2013-3-13.' >>> sub() 函数中的第一个参数是被匹配的模式,第二个参数是替换模式。反斜杠数字比如 \3 指向前面模式的捕获组号。
7.你想去掉文本字符串开头,结尾或者中间不想要的字符,比如空白。
strip() 方法能用于删除开始或结尾的字符。 lstrip() 和 rstrip() 分别从左和从右执行删除操作。 默认情况下,这些方法会去除空白字符,但是你也可以指定其他字符。比如:>>> # Whitespace stripping >>> s = ' hello world \n' >>> s.strip() 'hello world' >>> s.lstrip() 'hello world \n' >>> s.rstrip() ' hello world' >>> >>> # Character stripping >>> t = '-----hello=====' >>> t.lstrip('-') 'hello=====' >>> t.strip('-=') 'hello' >>>
8.你想通过某种对齐方式来格式化字符串
对于基本的字符串对齐操作,可以使用字符串的 ljust() , rjust() 和 center() 方法。比如:>>> text = 'Hello World' >>> text.ljust(20) 'Hello World ' >>> text.rjust(20) ' Hello World' >>> text.center(20) ' Hello World ' >>>
所有这些方法都能接受一个可选的填充字符。比如:
>>> text.rjust(20,'=') '=========Hello World' >>> text.center(20,'*') '****Hello World*****' >>>
函数 format() 同样可以用来很容易的对齐字符串。 你要做的就是使用 <,> 或者 ^ 字符后面紧跟一个指定的宽度。比如:
>>> format(text, '>20') ' Hello World' >>> format(text, '<20') 'Hello World ' >>> format(text, '^20') ' Hello World ' >>>
如果你想指定一个非空格的填充字符,将它写到对齐字符的前面即可:
>>> format(text, '=>20s') '=========Hello World' >>> format(text, '*^20s') '****Hello World*****' >>>
当格式化多个值的时候,这些格式代码也可以被用在 format() 方法中。比如:
>>> '{:>10s} {:>10s}'.format('Hello', 'World') ' Hello World' >>>
format() 函数的一个好处是它不仅适用于字符串。它可以用来格式化任何值,使得它非常的通用。 比如,你可以用它来格式化数字:
>>> x = 1.2345 >>> format(x, '>10') ' 1.2345' >>> format(x, '^10.2f') ' 1.23 ' >>>
9.你想创建一个内嵌变量的字符串,变量被它的值所表示的字符串替换掉。
Python并没有对在字符串中简单替换变量值提供直接的支持。 但是通过使用字符串的 format() 方法来解决这个问题。比如:>>> s = '{name} has {n} messages.' >>> s.format(name='Guido', n=37) 'Guido has 37 messages.' >>>
或者,如果要被替换的变量能在变量域中找到, 那么你可以结合使用 format_map() 和 vars() 。就像下面这样:
>>> name = 'Guido' >>> n = 37 >>> s.format_map(vars()) 'Guido has 37 messages.' >>>
10、你有一些长字符串,想以指定的列宽将它们重新格式化。
使用 textwrap 模块来格式化字符串的输出。比如,假如你有下列的长字符串:s = "Look into my eyes, look into my eyes, the eyes, the eyes, \ the eyes, not around the eyes, don't look around the eyes, \ look into my eyes, you're under."
下面演示使用 textwrap 格式化字符串的多种方式:
>>> import textwrap >>> print(textwrap.fill(s, 70)) Look into my eyes, look into my eyes, the eyes, the eyes, the eyes, not around the eyes, don't look around the eyes, look into my eyes, you're under. >>> print(textwrap.fill(s, 40, initial_indent=' ')) Look into my eyes, look into my eyes, the eyes, the eyes, the eyes, not around the eyes, don't look around the eyes, look into my eyes, you're under.
cookies:
判断给定的日期字符串是否匹配某种模式
re.match(r'^\d+/\d+/\d+$', dateStr) re.match(r'^\d+-\d+-\d+$', dateStr)
相关文章推荐
- 其他文本处理命令以及bash变量中的一些字符串的处理方法
- 【脚本语言系列】关于Python操作数据文本字符串,你需要知道的事
- 关于Python中空格字符串处理的技巧总结
- cookbook-第二章-字符串和文本处理
- c语言中关于字符串的一些常规处理
- 关于PHP的一些常用的字符串处理函数
- 自己整理的关于C的一些字符串处理函数
- java处理常规中文文本:给每行添加一些字符串
- Python+selenium 关于富文本形式的评论框处理方法
- 关于SQL字符串处理的一些函数
- Python3 CookBook | 字符串和文本
- python字符串处理方式-python学习笔记29
- js 关于中文字符串的一些处理
- C# 关于字符、字符串和文本处理
- Python——一些关于处理列表的函数
- Python实现简单文本字符串处理的方法
- Python四钟方式来实现“一次处理一个字符的方式处理字符串”
- 关于机器学习中文本处理的一些常用方法
- 用python处理文本数据 学到的一些东西
- 【脚本语言系列】关于Python基础知识处理字符串,你需要知道的事