【Python编码规范】库

本系列为《编写高质量代码-改善Python程序的91个建议》的读书笔记。

温馨提醒:在阅读本书之前,强烈建议先仔细阅读:PEP规范,增强代码的可阅读性,配合优雅的pycharm编辑器(开启pep8检查)写出规范代码,是Python入门的第一步。

建议36:掌握字符串的基本用法

Python小技巧Python遇到未闭合的小括号会自动将多行代码拼接为一行和把相邻的两个字符串字面量拼接在一起的。

>>> st = ('select * '
...       'from table '
...       'whre field="value";')
>>> st
'select * from table whre field="value";'
  • 字符串用法举例:
>>> print isinstance('hello world', basestring)  # basestring是str与unicode的基类
True
>>> print isinstance('hello world', unicode)
False
>>> print isinstance('hello world', str)
True
>>> print isinstance(u'hello world', unicode)
True
  • split()的陷阱示例
>>> ' Hello World'.split(' ')
['', 'Hello', 'World']
>>> ' Hello   World'.split()
['Hello', 'World']
>>> ' Hello   World'.split(' ')
['', 'Hello', '', '', 'World']
  • title()应用示例
>>> import string
>>> string.capwords('hello  wOrld')
'Hello World'
>>> string.capwords(' hello  wOrld ')
'Hello World'
>>> ' hello  wOrld '.title()
' Hello  World '

建议37:按需选择sort()或者sorted()

sorted(iterable[, cmp[, key[, reverse]]]):作用于任何可迭代对象,返回一个排序后的列表;

sort(cmp[, key[, reverse]]]):一般作用于列表,直接修改原有列表,返回为None

1)对字典进行排序

>>> from operator import itemgetter
>>> phone_book = {
   'Linda': '775', 'Bob': '9349', 'Carol': '5834'}
>>> sorted_pb = sorted(phone_book.iteritems(), key=itemgetter(1))# 按照字典的value进行排序
>>> print phone_book
{
   'Linda': '775', 'Bob': '9349', 'Carol': '5834'}
>>> print sorted_pb
[('Carol', '5834'), ('Linda', '775'), ('Bob', '9349')]

2)多维list排序

>>> from operator import itemgetter
>>> game_result = [['Linda', 95, 'B'], ['Bob', 93, 'A'], ['Carol', 69, 'D'], ['zhangs', 95, 'A']]
>>> sorted_res = sorted(game_result, key=itemgetter(1, 2))  # 按照学生成绩排序,成绩相同的按照等级排序
>>> print game_result
[['Linda', 95, 'B'], ['Bob', 93, 'A'], ['Carol', 69, 'D'], ['zhangs', 95, 'A']]
>>> print sorted_res
[['Carol', 69, 'D'], ['Bob', 93, 'A'], ['zhangs', 95, 'A'], ['Linda', 95, 'B']]

3)字典中混合list排序

>>> from operator import itemgetter
>>> list_dict = {
   
...     'Li': ['M', 7],
...     'Zhang': ['E', 2],
...     'Du': ['P', 3],
...     'Ma': ['C', 9],
...     'Zhe': ['H', 7]
... }
>>> sorted_ld = sorted(list_dict.iteritems(), key=lambda (k, v): itemgetter(1)(v))  # 按照字典的value[m,n]中的n值排序
>>> print list_dict
{
   'Zhe': ['H', 7], 'Zhang': ['E', 2], 'Ma': ['C', 9], 'Du': ['P', 3], 'Li': ['M', 7]}
>>> print sorted_ld
[('Zhang', ['E', 2]), ('Du', ['P', 3]), ('Zhe', ['H', 7]), ('Li', ['M', 7]), ('Ma', ['C', 9])]

4)list中混合字典排序

>>> from operator import itemgetter
>>> game_result = [
...     {
   'name': 'Bob', 'wins': 10, 'losses': 3, 'rating': 75},
...     {
   'name': 'David', 'wins': 3, 'losses': 5, 'rating': 57},
...     {
   'name': 'Carol', 'wins': 4, 'losses': 5, 'rating': 57},
...     {
   'name': 'Patty', 'wins': 9, 'losses': 3, 'rating': 71.48}
... ]
>>> sorted_res = sorted(game_result, key=itemgetter('rating', 'name'))   # 按照name和rating排序
>>> print game_result
[{
   'wins': 10, 'losses': 3, 'name': 'Bob', 'rating': 75}, {
   'wins': 3, 'losses': 5, 'name': 'David', 'rating': 57}, 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值