广告位联系
返回顶部
分享到

分享10提高Python代码的可读性的技巧

python 来源:互联网 作者:秩名 发布时间:2022-03-03 17:43:58 人浏览
摘要

1. 字符串反转 字符串反转有很多方法,咱们再这里介绍两种:一种是切片,一种是python字符串的reversed方法。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 # -!- coding: utf-8 -!- string = hello world # 方法1 new_

1. 字符串反转

字符串反转有很多方法,咱们再这里介绍两种:一种是切片,一种是python字符串的reversed方法。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

# -!- coding: utf-8 -!-

string = 'hello world'

 

# 方法1

new_str = string[::-1]

ic(new_str)

 

# 方法二

new_str2 = ''.join(reversed(string))

ic(new_str2)

 

'''

ic| new_str: 'dlrow olleh'

ic| new_str2: 'dlrow olleh'

'''

2. 首字母大写

这里咱们也是介绍两种方法,区别之处在于**capitalize()**仅是首字母大写

**title()**是每个单词开头的首字母都大写

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

# 首字母大写

string = 'hello python and world'

 

# 方法一

new_str = string.capitalize()

ic(new_str)

 

 

# 方法二

new_str2 = string.title()

ic(new_str2)

 

'''

ic| new_str: 'Hello python and world'

ic| new_str2: 'Hello Python And World'

'''

3. 查询唯一元素

我们利用set的唯一性来确定字符串的唯一元素:

1

2

3

4

5

6

7

8

9

10

11

12

string = 'hellohellohello'

new_str = set(string)

# set类型

ic(new_str)

# 字符串类型

new_str = ''.join(new_str)

ic(new_str)

 

'''

ic| new_str: {'l', 'o', 'h', 'e'}

ic| new_str: 'lohe'

'''

4. 变量交换

python中的变量交换比java简单多了,交换两个变量无需定义第三个中间变量,直接交换即可实现

1

2

3

4

5

6

7

8

9

10

11

12

a = 'hello'

b = 'world'

ic(a+b)

 

# 直接交换两个变量

a, b = b, a

ic(a+b)

 

'''

ic| a+b: 'helloworld'

ic| a+b: 'worldhello'

'''

5. 列表排序

列表排序这里我们也提供两种方式。第一个是列表自带的**sort()方法;第二个是python内置函数sorted()**方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

score = [88, 99, 91, 85, 94, 85, 94, 78, 100, 80]

# 方法一

new_score = sorted(score)

ic('默认升序:', new_score)

 

score = [57, 29, 11, 27, 84, 34, 87, 25, 70, 60]

# 方法二

new_score2 = sorted(score, reverse=True)

ic('设置降序', new_score2)

 

'''

ic| '默认升序:', new_score: [78, 80, 85, 85, 88, 91, 94, 94, 99, 100]

ic| '设置降序', new_score2: [87, 84, 70, 60, 57, 34, 29, 27, 25, 11]

'''

6.列表推导式

使用列表推导式可以快速生成一个列表或者根据列表生成满足需求的列表

1

2

3

4

5

6

7

8

9

10

11

12

13

# 生成10个10-100以内随机整数

numbers = [random.randint(10, 100) for x in range(10)]

ic(numbers)

 

# 输入5折后的价格

price = [800, 500, 400, 860, 780, 520, 560]

half_price = [(x*0.5)for x in price]

ic(half_price)

 

'''

ic| numbers: [64, 22, 80, 70, 34, 81, 74, 35, 85, 12]

ic| half_price: [400.0, 250.0, 200.0, 430.0, 390.0, 260.0, 280.0]

'''

7. 合并字符串

合并字符串我们使用string的.join()方法实现

1

2

3

4

5

6

7

8

9

lists = ['hello', 'world', 'python', 'java', 'c++']

 

# 合并字符串

new_str = ' '.join(lists)

ic(new_str)

 

'''

ic| new_str: 'hello world python java c++'

'''

8. 拆分字符串

拆分字符串我们使用string的split()方法实现

1

2

3

4

5

6

7

8

9

10

11

12

13

14

string = 'hello world python java c++'

string2 = 'hello|world|python|java|c++'

 

# 拆分字符串

new_str = string.split(' ')

ic(new_str)

 

new_str2 = string2.split('|')

ic(new_str2)

 

'''

ic| new_str: ['hello', 'world', 'python', 'java', 'c++']

ic| new_str2: ['hello', 'world', 'python', 'java', 'c++']

'''

9. 回文串检测

回文串是指aba、abba、cccbccc、aaaa这种左右对称的字符串。我们可以根据之前提到的切片来检测这种特殊的字符串序列

1

2

3

4

5

6

7

8

9

10

str = '20211202'

 

if str == str[::-1]:

    print('yes')

else:

    print('no')

 

'''

yes

'''

10. 统计列表元素出现次数

统计列表中元素各自出现的次数我们使用collections 的Counter方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

from collections import Counter

lists = ['a', 'a', 'b', 'b', 'b', 'c', 'd', 'd', 'd', 'd', 'd']

 

# 统计所有元素出现的次数

counts = Counter(lists)

ic(counts)

 

# 统计某一元素出现的次数

ic(counts['d'])

 

# 统计出现最多次数的一个元素

ic(counts.most_common(1))

 

'''

ic| counts: Counter({'d': 5, 'b': 3, 'a': 2, 'c': 1})

ic| counts['d']: 5

ic| counts.most_common(1): [('d', 5)]

'''


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.csdn.net/weixin_38037405/article/details/121755149
相关文章
  • Python Django教程之实现新闻应用程序

    Python Django教程之实现新闻应用程序
    Django是一个用Python编写的高级框架,它允许我们创建服务器端Web应用程序。在本文中,我们将了解如何使用Django创建新闻应用程序。 我们将
  • 书写Python代码的一种更优雅方式(推荐!)

    书写Python代码的一种更优雅方式(推荐!)
    一些比较熟悉pandas的读者朋友应该经常会使用query()、eval()、pipe()、assign()等pandas的常用方法,书写可读性很高的「链式」数据分析处理代码
  • Python灰度变换中伽马变换分析实现

    Python灰度变换中伽马变换分析实现
    1. 介绍 伽马变换主要目的是对比度拉伸,将图像灰度较低的部分进行修正 伽马变换针对的是对单个像素点的变换,也就是点对点的映射 形
  • 使用OpenCV实现迷宫解密的全过程

    使用OpenCV实现迷宫解密的全过程
    一、你能自己走出迷宫吗? 如下图所示,可以看到是一张较为复杂的迷宫图,相信也有人尝试过自己一点一点的找出口,但我们肉眼来解谜
  • Python中的数据精度问题的介绍

    Python中的数据精度问题的介绍
    一、python运算时精度问题 1.运行时精度问题 在Python中(其他语言中也存在这个问题,这是计算机采用二进制导致的),有时候由于二进制和
  • Python随机值生成的常用方法

    Python随机值生成的常用方法
    一、随机整数 1.包含上下限:[a, b] 1 2 3 4 import random #1、随机整数:包含上下限:[a, b] for i in range(10): print(random.randint(0,5),end= | ) 查看运行结
  • Python字典高级用法深入分析讲解
    一、 collections 中 defaultdict 的使用 1.字典的键映射多个值 将下面的列表转成字典 l = [(a,2),(b,3),(a,1),(b,4),(a,3),(a,1),(b,3)] 一个字典就是一个键对
  • Python浅析多态与鸭子类型使用实例
    什么多态:同一事物有多种形态 为何要有多态=》多态会带来什么样的特性,多态性 多态性指的是可以在不考虑对象具体类型的情况下而直
  • Python字典高级用法深入分析介绍
    一、 collections 中 defaultdict 的使用 1.字典的键映射多个值 将下面的列表转成字典 l = [(a,2),(b,3),(a,1),(b,4),(a,3),(a,1),(b,3)] 一个字典就是一个键对
  • Python淘宝或京东等秒杀抢购脚本实现(秒杀脚本

    Python淘宝或京东等秒杀抢购脚本实现(秒杀脚本
    我们的目标是秒杀淘宝或京东等的订单,这里面有几个关键点,首先需要登录淘宝或京东,其次你需要准备好订单,最后要在指定时间快速
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计