会员登录 - 用户注册 - 设为首页 - 加入收藏 - 网站地图 提高 Python 代码的可读性,你需要知道的十个技巧!

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

时间:2025-11-05 16:01:51 来源:益强数据堂 作者:人工智能 阅读:185次

 1. 字符串反转

字符串反转有很多方法,提高咱们再这里介绍两种:一种是代码的可读性道切片,一种是需知python字符串的reversed方法。 

# -!- 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()**是每个单词开头的首字母都大写 

# 首字母大写 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的唯一性来确定字符串的唯一元素: 

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简单多了,交换两个变量无需定义第三个中间变量,亿华云计算代码的可读性道直接交换即可实现。需知 

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()**方法。 

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.列表推导式

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

# 生成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()方法实现。 

lists = [hello,个技 world, python, java, c++] # 合并字符串 new_str =  .join(lists) ic(new_str) ic| new_str: hello world python java c++ 

8. 拆分字符串

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

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这种左右对称的字符串。我们可以根据之前提到的切片来检测这种特殊的字符串序列。 

str = 20211202 if str == str[::-1]:     print(yes) else:     print(no) yes 

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

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

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)] IT技术网

(责任编辑:IT科技类资讯)

推荐内容
  • 安装命令: 功能说明:检查、设定系统的各种服务。 语法:chkconfig [--add][--del][--list][系统服务]或 补充说明:这个是redhat公司遵循gpl规则所开发的程序,它可以查询操作系统在每一个执行等级(runlevel)中,会执行哪些系统服务,其中包括各种daemon。 linux os 将操作环境分为以下7个等级: 0:开机(请不要切换到此等级) 参数: --add 新增所指定的系统服务 chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息。谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接。 语法: chkconfig --list [name] chkconfig 没有参数运行时,显示用法。假如加上服务名,那么就检查这个服务是否在当前运行级启动。假如是,返回true,否则返回false。假如在服务名后面指定 了on,off或者reset,那么chkconfi 会改变指定服务的启动信息。On和off分别指服务被启动和停止,reset指重置服务的启动信息,无论有问题的初始化脚本指定了什么。On和off开 关,系统默认只对运行级3,4,5有效,但是reset可以对所有运行级有效。 --level选项可以指定要查看的运行级而不一定是当前运行级。 需要说明的是,对于每个运行级,只能有一个启动脚本或者停止脚本。当切换运行级时,init不会重新启动已经启动的服务,也不会再次去停止已经停止的服务。 chkconfig --list :显示所有运行级系统服务的运行状态信息(on或off)。假如指定了name,那么只显示指定的服务在不同运行级的状态。 chkconfig --add name:增加一项新的服务。Chkconfig确保每个运行级有一项启动(S)或者杀死(K)入口。如有缺少,则会从缺省的init脚本自动建立。 chkconfig --del name:删除服务,并把相关符号连接从/etc/rc[0-6].d删除。 chkconfig [--level levels] name :设置某一服务在指定的运行级是被启动,停止还是重置。例如,要在3,4,5运行级停止nfs服务,则命令如下: chkconfig --level 345 nfs off 运行级文件: 每个被chkconfig管理的服务需要在对应的init.d下的脚本加上两行或者更多行的注释。第一行告诉chkconfig缺省启动的运行级以及启动 和停止的优先级。假如某服务缺省不在任何运行级启动,那么使用 - 代替运行级。第二行对服务进行描述,可以用/ 跨行注释。 附加介绍一下Linux系统的运行级的概念: 范例: 1.查看在各种不同的执行等级中,各项服务的状况: 2.列出系统服务vsftpd在各个执行等级的启动情况: 3.在执行等级3,5时,关闭vsftpd系统服务: 4.在执行等级2,3,5时,开启 vsftpd系统服务: 5.关闭一些自己不需要的服务 ->假如没有打印机: chkconfig --level 235 cups off 假如没有局域网: chkconfig --level 235 smb off 假如不需要远程用户登录的: chkconfig --level 235 sshd off 假如不需要定时任务的: chkconfig --level 235 crond off 假如不需要添加新硬件的: chkconfig --level 235 kudzu off
  • 删库跑路大神的一生:曾在家造炸弹被捕,原是开源创业之星
  • 微服务难点剖析 | 服务拆的挺爽,问题是日志该怎么串联起来呢?
  • 段云峰:DB2 9对企业有3点帮助
  • 解决电脑开机蓝屏错误的有效方法(针对电脑开机蓝屏错误的解决方案)
  • 注册域名的网站有哪些?