Python之每个人都应该知道的30个一行代码程序

举报
Serendipity·y 发表于 2022/02/17 00:29:27 2022/02/17
【摘要】 Python 语法简洁,能够用一行代码实现很多有趣的功能,现在整理 30 个常见的 Python 一行代码集合。 ① 转置矩阵 old_list = [[1, 2, 3], [3, 4, 6], [5...

Python 语法简洁,能够用一行代码实现很多有趣的功能,现在整理 30 个常见的 Python 一行代码集合。

① 转置矩阵

old_list = [[1, 2, 3], [3, 4, 6], [5, 6, 7]]
list(list(x) for x in zip(*old_list))
[[1, 3, 5], [2, 4, 6], [3, 6, 7]]

  
 

② 二进制转十进制

decimal = int('1010', 2)
print(decimal) #10
10

  
 

③ 字符串大写转小写

# 方法一 lower()
"Hi my name is Allwin".lower()
# 'hi my name is allwin'
# 方法二 casefold()
"Hi my name is Allwin".casefold()
# 'hi my name is allwin'
'hi my name is allwin'

  
 

④ 字符串小写转大写

"hi my name is Allwin".upper()
# 'HI MY NAME IS ALLWIN'
'HI MY NAME IS ALLWIN'

  
 

⑤ 将字符串转换为字节

"convert string to bytes using encode method".encode()
# b'convert string to bytes using encode method'
b'convert string to bytes using encode method'

  
 

⑥ 复制文件内容

import shutil; shutil.copyfile('source.txt', 'dest.txt')
'dest.txt'

  
 

⑦ 快速排序

qsort = lambda l : l if len(l)<=1 else qsort([x for x in l[1:] if x < l[0]]) + [l[0]] + qsort([x for x in l[1:] if x >= l[0]])
qsort([1,3,2])
[1, 2, 3]

  
 

⑧ n 个连续数之和

n = 3
sum(range(0, n+1))
6

  
 

⑨ 交换两个变量

a=1
b=2
a,b = b,a

  
 

⑩ 斐波那契数列

fib = lambda x: x if x<=1 else fib(x-1) + fib(x-2)
fib(10)
55

  
 

⑪ 将嵌套列表合并为一个列表

main_list = [[1,2],[3,4],[5,6,7]]
[item for sublist in main_list for item in sublist]
[1, 2, 3, 4, 5, 6, 7]

  
 

⑫ 运行 HTTP 服务器

python3 -m http.server 8000

  
 

⑬ 反转列表

numbers = 'I Love China'
numbers[::-1]
'anihC evoL I'

  
 

⑭ 返回阶乘

import math; fact_5 = math.factorial(5)
fact_5
120

  
 

⑮ 判断列表推导式

even_list = [number for number in [1, 2, 3, 4] if number % 2 == 0]
even_list
[2, 4]

  
 

⑯ 取最长字符串

words = ['This', 'is', 'a', 'list', 'of', 'words']
max(words, key=len) 
'words'

  
 

⑰ 列表推导式

li = [num for num in range(0,100)]
# this will create a list of numbers from 0 to 99

  
 

⑱ 集合推导式

num_set = { num for num in range(0,100)}
# this will create a set of numbers from 0 to 99

  
 

⑲ 字典推导式

dict_numbers = {x:x*x for x in range(1,5) }
# {1: 1, 2: 4, 3: 9, 4: 16}

  
 

⑳ if-else

print("even") if 4%2==0 else print("odd")
even

  
 

㉑ 无限循环

while 1:0

  
 

㉒ 检查数据类型

isinstance(2, int)
isinstance("allwin", str)
isinstance([3,4,1997], list)

  
 

㉓ while 循环

a=5
while a > 0: a = a - 1; print(a)

  
 

㉔ 使用 print 语句写入到文件里

print("Hello, World!", file=open('source.txt', 'w'))

  
 

㉕ 统计字频

print("umbrella".count('l'))
2

  
 

㉖ 合并两个列表

list1.extend(list2)
# contents of list 2 will be added to the list1

  
 

㉗ 合并两个字典

dict1.update(dict2)
# contents of dictionary 2 will be added to the dictionary 1

  
 

㉘ 合并两个集合

set1.update(set2)
# contents of set2 will be copied to the set1

  
 

㉙ 时间戳

import time; print(time.time())
1632146103.8406303

  
 

㉚ 统计最多的元素

test_list = [9, 4, 5, 4, 4, 5, 9, 5, 4]
most_frequent_element = max(set(test_list), key=test_list.count)
most_frequent_element

  
 

最后,Python 代码哲学崇尚简洁,伙伴们也可以尝试把代码简化,看能不能实现想要的功能。

文章来源: blog.csdn.net,作者:Serendipity·y,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/Forever_wj/article/details/120607794

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。