Python常用技巧

1、推导式

推导式又叫做解析式,是简化冗余代码实质有效的方法,Python中实用的语法糖,可以分为以下几类:

  1. 列表推导式(⭐)
  2. 字典推导式(⭐)
  3. 集合推导式
  4. 元祖推导式

下面只介绍常用的列表推导式和字典推导式。

1.1 列表推导式

对列表中每个元素进行一定操作。

基本使用

1
2
3
4
list = [1,2,3,4,5,6,7,8,9,10]
list1 = [x+2 for x in list]
print(list1)
# [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
PYTHON

条件表达式

1
2
3
4
list = ["Bob","Jason","Peter","Jani","Tom"]
list1 = [name+"666" for name in list if len(name) > 3]
print(list1)
# ['Jason666', 'Peter666', 'Jani666']
PYTHON

多个 for 循环

1
2
3
time = ['%.2d:%.2d'%(h,m )for h in range(24) for m in range(0,60,5) ]
print(time)
# ['00:00', '00:05', '00:10', '00:15', '00:20', '00:25', '00:30', '00:35', '00:40', '00:45', '00:50', '00:55', '01:00', '01:05', '01:10', '01:15', '01:20', '01:25', '01:30', '01:35', '01:40', '01:45', '01:50', '01:55', '02:00', '02:05', '02:10', '02:15', '02:20', '02:25', '02:30', '02:35', '02:40', '02:45', '02:50', '02:55', '03:00', '03:05', '03:10', '03:15', '03:20', '03:25', '03:30', '03:35', '03:40', '03:45', '03:50', '03:55', '04:00', '04:05', '04:10', '04:15', '04:20', '04:25', '04:30', '04:35', '04:40', '04:45', '04:50', '04:55', '05:00', '05:05', '05:10', '05:15', '05:20', '05:25', '05:30', '05:35', '05:40', '05:45', '05:50', '05:55', '06:00', '06:05', '06:10', '06:15', '06:20', '06:25', '06:30', '06:35', '06:40', '06:45', '06:50', '06:55', '07:00', '07:05', '07:10', '07:15', '07:20', '07:25', '07:30', '07:35', '07:40', '07:45', '07:50', '07:55', '08:00', '08:05', '08:10', '08:15', '08:20', '08:25', '08:30', '08:35', '08:40', '08:45', '08:50', '08:55', '09:00', '09:05', '09:10', '09:15', '09:20', '09:25', '09:30', '09:35', '09:40', '09:45', '09:50', '09:55', '10:00', '10:05', '10:10', '10:15', '10:20', '10:25', '10:30', '10:35', '10:40', '10:45', '10:50', '10:55', '11:00', '11:05', '11:10', '11:15', '11:20', '11:25', '11:30', '11:35', '11:40', '11:45', '11:50', '11:55', '12:00', '12:05', '12:10', '12:15', '12:20', '12:25', '12:30', '12:35', '12:40', '12:45', '12:50', '12:55', '13:00', '13:05', '13:10', '13:15', '13:20', '13:25', '13:30', '13:35', '13:40', '13:45', '13:50', '13:55', '14:00', '14:05', '14:10', '14:15', '14:20', '14:25', '14:30', '14:35', '14:40', '14:45', '14:50', '14:55', '15:00', '15:05', '15:10', '15:15', '15:20', '15:25', '15:30', '15:35', '15:40', '15:45', '15:50', '15:55', '16:00', '16:05', '16:10', '16:15', '16:20', '16:25', '16:30', '16:35', '16:40', '16:45', '16:50', '16:55', '17:00', '17:05', '17:10', '17:15', '17:20', '17:25', '17:30', '17:35', '17:40', '17:45', '17:50', '17:55', '18:00', '18:05', '18:10', '18:15', '18:20', '18:25', '18:30', '18:35', '18:40', '18:45', '18:50', '18:55', '19:00', '19:05', '19:10', '19:15', '19:20', '19:25', '19:30', '19:35', '19:40', '19:45', '19:50', '19:55', '20:00', '20:05', '20:10', '20:15', '20:20', '20:25', '20:30', '20:35', '20:40', '20:45', '20:50', '20:55', '21:00', '21:05', '21:10', '21:15', '21:20', '21:25', '21:30', '21:35', '21:40', '21:45', '21:50', '21:55', '22:00', '22:05', '22:10', '22:15', '22:20', '22:25', '22:30', '22:35', '22:40', '22:45', '22:50', '22:55', '23:00', '23:05', '23:10', '23:15', '23:20', '23:25', '23:30', '23:35', '23:40', '23:45', '23:50', '23:55']
PYTHON

多个 for 循环+条件表达式

1
2
3
list = [(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
print(list)
# [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
PYTHON

1.2 字典推导式

1
2
3
4
listdemo = ["Bob","Jason","Peter","Jani","Tom"]
newdict = {key:len(key) for key in listdemo}
print(newdict)
# {'Bob': 3, 'Jason': 5, 'Peter': 5, 'Jani': 4, 'Tom': 3}
PYTHON

互换 key 和 value 的值

1
2
3
4
person = {'角色名':'宫本武藏','定位':'刺客'}
person_reverse = {k:v for v,k in person.items()}
print(person_reverse)
# {'宫本武藏': '角色名', '刺客': '定位'}
PYTHON

2、字典遍历

1
2
3
person = {'角色名':'宫本武藏','定位':'刺客'}
for key,value in person.items():
print(key,value)
PYTHON

3、判断元素是否在列表中

1
2
3
4
listdemo = ["Bob","Jason","Peter","Jani","Tom"]
if "Peter" in listdemo:
print("Yes")
# Yes
PYTHON

4、反转字符串

1
2
3
4
name = "Tom"
reverse_name = name[::-1]
print(reverse_name)
# moT
PYTHON

5、反转列表

1
2
3
4
name_list = ["Bob","Jason","Peter","Jani","Tom"]
reverse_name_list = name_list[::-1]
print(reverse_name_list)
# ['Tom', 'Jani', 'Peter', 'Jason', 'Bob']
PYTHON

6、获取时间

获取当前时间

1
2
3
4
import datetime
current_time = datetime.datetime.now()
print(current_time)
# 2024-04-24 23:59:57.567560
PYTHON

获取当前日期

1
2
3
4
import datetime
current_date = datetime.date.today()
print("当前日期:", current_date)
# 当前日期: 2024-04-25
PYTHON

获取毫秒级时间(程序执行时间)

1
2
3
4
5
6
7
import time
start_time = time.time()
numbers = [i for i in range(1000000)]
end_time = time.time()
time_ = end_time - start_time
print(time_)
# 0.03652048110961914
PYTHON

7、三元运算符

1
2
3
4
5
a = 1
b = 2
max = a if a>b else b
print(max)
# 2
PYTHON

8、枚举循环

1
2
3
4
5
fruits = ['apple','banana']
for index,fruit in enumerate(fruits):
print(index,fruit)
# 0 apple
# 1 banana
PYTHON

Python常用技巧
http://example.com/2024/04/24/017 Python常用技巧分享/
作者
DB
发布于
2024年4月24日
许可协议