51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1126|回复: 1
打印 上一主题 下一主题

[转贴] 提高 Python 代码的可读性,你需要知道的十个技巧

[复制链接]
  • TA的每日心情
    无聊
    前天 09:10
  • 签到天数: 959 天

    连续签到: 5 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2021-12-10 13:47:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
     1. 字符串反转
      字符串反转有很多方法,咱们再这里介绍两种:一种是切片,一种是python字符串的reversed方法。
    1.  # -!- coding: utf-8 -!-
    2.   string = 'hello world'
    3.   
    4.   # 方法1
    5.   new_str = string[::-1]
    6.   ic(new_str)
    7.   
    8.   # 方法二
    9.   new_str2 = ''.join(reversed(string))
    10.   ic(new_str2)
    11.   
    12.   '''
    13.   ic| new_str: 'dlrow olleh'
    14.   ic| new_str2: 'dlrow olleh'
    15.   '''
    复制代码

    2. 首字母大写
      这里咱们也是介绍两种方法,区别之处在于**capitalize()**仅是首字母大写。
      **title()**是每个单词开头的首字母都大写。
    1. # 首字母大写
    2.   string = 'hello python and world'
    3.   
    4.   # 方法一
    5.   new_str = string.capitalize()
    6.   ic(new_str)
    7.   
    8.   
    9.   # 方法二
    10.   new_str2 = string.title()
    11.   ic(new_str2)
    12.   
    13.   '''
    14.   ic| new_str: 'Hello python and world'
    15.   ic| new_str2: 'Hello Python And World'
    16.   '''
    复制代码

    3. 查询唯一元素
      我们利用set的唯一性来确定字符串的唯一元素:
    1.  string = 'hellohellohello'
    2.   new_str = set(string)
    3.   # set类型
    4.   ic(new_str)
    5.   # 字符串类型
    6.   new_str = ''.join(new_str)
    7.   ic(new_str)
    8.   
    9.   '''
    10.   ic| new_str: {'l', 'o', 'h', 'e'}
    11.   ic| new_str: 'lohe'
    12.   '''
    复制代码

    4. 变量交换
      python中的变量交换比java简单多了,交换两个变量无需定义第三个中间变量,直接交换即可实现。
    1.  a = 'hello'
    2.   b = 'world'
    3.   ic(a+b)
    4.   
    5.   # 直接交换两个变量
    6.   a, b = b, a
    7.   ic(a+b)
    8.   
    9.   '''
    10.   ic| a+b: 'helloworld'
    11.   ic| a+b: 'worldhello'
    12.   '''
    复制代码

    5. 列表排序
      列表排序这里我们也提供两种方式。第一个是列表自带的**sort() 方法;第二个是python内置函数 sorted()**方法。
    1. score = [88, 99, 91, 85, 94, 85, 94, 78, 100, 80]
    2.   # 方法一
    3.   new_score = sorted(score)
    4.   ic('默认升序:', new_score)
    5.   
    6.   score = [57, 29, 11, 27, 84, 34, 87, 25, 70, 60]
    7.   # 方法二
    8.   new_score2 = sorted(score, reverse=True)
    9.   ic('设置降序', new_score2)
    10.   
    11.   '''
    12.   ic| '默认升序:', new_score: [78, 80, 85, 85, 88, 91, 94, 94, 99, 100]
    13.   ic| '设置降序', new_score2: [87, 84, 70, 60, 57, 34, 29, 27, 25, 11]
    14.   '''
    复制代码

    6.列表推导式
      使用列表推导式可以快速生成一个列表或者根据列表生成满足需求的列表。
    1.   # 生成10个10-100以内随机整数
    2.   numbers = [random.randint(10, 100) for x in range(10)]
    3.   ic(numbers)
    4.   
    5.   # 输入5折后的价格
    6.   price = [800, 500, 400, 860, 780, 520, 560]
    7.   half_price = [(x*0.5)for x in price]
    8.   ic(half_price)
    9.   
    10.   '''
    11.   ic| numbers: [64, 22, 80, 70, 34, 81, 74, 35, 85, 12]
    12.   ic| half_price: [400.0, 250.0, 200.0, 430.0, 390.0, 260.0, 280.0]
    13.   '''
    复制代码

    7. 合并字符串
      合并字符串我们使用string的.join()方法实现。
    1. lists = ['hello', 'world', 'python', 'java', 'c++']
    2.   
    3.   # 合并字符串
    4.   new_str = ' '.join(lists)
    5.   ic(new_str)
    6.   
    7.   '''
    8.   ic| new_str: 'hello world python java c++'
    9.   '''
    复制代码

    8. 拆分字符串
      拆分字符串我们使用string的split()方法实现。
    1. string = 'hello world python java c++'
    2.   string2 = 'hello|world|python|java|c++'
    3.   
    4.   # 拆分字符串
    5.   new_str = string.split(' ')
    6.   ic(new_str)
    7.   
    8.   new_str2 = string2.split('|')
    9.   ic(new_str2)
    10.   
    11.   '''
    12.   ic| new_str: ['hello', 'world', 'python', 'java', 'c++']
    13.   ic| new_str2: ['hello', 'world', 'python', 'java', 'c++']
    14.   '''
    复制代码

    9. 回文串检测
      回文串是指aba、abba、cccbccc、aaaa这种左右对称的字符串。我们可以根据之前提到的切片来检测这种特殊的字符串序列。
    1.  str = '20211202'
    2.   
    3.   if str == str[::-1]:
    4.       print('yes')
    5.   else:
    6.       print('no')
    7.   
    8.   '''
    9.   yes
    10.   '''
    复制代码

    10. 统计列表元素出现次数
      统计列表中元素各自出现的次数我们使用collections 的Counter方法。
    1. from collections import Counter
    2.   lists = ['a', 'a', 'b', 'b', 'b', 'c', 'd', 'd', 'd', 'd', 'd']
    3.   
    4.   # 统计所有元素出现的次数
    5.   counts = Counter(lists)
    6.   ic(counts)
    7.   
    8.   # 统计某一元素出现的次数
    9.   ic(counts['d'])
    10.   
    11.   # 统计出现最多次数的一个元素
    12.   ic(counts.most_common(1))
    13.   
    14.   '''
    15.   ic| counts: Counter({'d': 5, 'b': 3, 'a': 2, 'c': 1})
    16.   ic| counts['d']: 5
    17.   ic| counts.most_common(1): [('d', 5)]
    18.   '''
    复制代码











    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2021-6-9 14:08
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    2#
    发表于 2021-12-14 09:04:43 | 只看该作者
    每天一个小技巧,提升python在路上
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-6-2 14:12 , Processed in 0.066681 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表