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 代码哲学崇尚简洁,伙伴们也可以尝试把代码简化,看能不能实现想要的功能。

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

  1. 每个人都应该知道的 18 个强大的 Excel 快捷键!

    Microsoft Excel 是一种程序,可让您在计算机或移动设备上高效地执行各种任务.它是全球小型和大型企业主要使用的重要工具. 如果您是一位经验丰富的用户,那么您可能知道 Excel 快捷键并正 ...

  2. 每个人都应该知道的25个大数据术语

    摘要: 如果你初来乍到,大数据看起来很吓人!根据你掌握的基本理论,让我们专注于一些关键术语以此给你的约会对象.老板.家人或者任何一个人带来深刻的印象. 让我们开始吧: 1.算法."算法&qu ...

  3. 每个人都应该知道的25个大数据术语 1

    摘要: 如果你初来乍到,大数据看起来很吓人!根据你掌握的基本理论,让我们专注于一些关键术语以此给你的约会对象.老板.家人或者任何一个人带来深刻的印象. 让我们开始吧: 1.算法."算法&qu ...

  4. 每个人都应该知道的15个大数据术语

    1.算法."算法"如何与大数据相关?即使算法是一个通用术语,但大数据分析使其在当代更受青睐和流行. 2.分析.年末你可能会收到一份来自信用卡公司寄来的包含了全年所有交易记录的年终报 ...

  5. 每个人都应该知道的Android Studio快捷方式

    Android Studio has a lot of shortcuts to offer. Today, we will be sharing some useful shortcuts whic ...

  6. 系统管理员都要知道的 30 个 Linux 系统监控工具

    1. top - 进程活动监控命令 top 命令会显示 Linux 的进程.它提供了一个运行中系统的实时动态视图,即实际的进程活动.默认情况下,它显示在服务器上运行的 CPU 占用率最高的任务,并且每 ...

  7. 每个系统管理员都要知道的 30 个 Linux 系统监控工具

    https://www.tuicool.com/articles/IzYNjyI 您需要监控 Linux 服务器的性能吗?试试用这些内置命令和附加工具吧!大多数 Linux 发行版都附带了大量的监控工 ...

  8. 资深程序员才知道的30个Python技巧

    Python中的省略号 Python省略号是三点序列,通常在自然语言中使用.但是您可能不知道的是,这也是Python中的有效对象: - Ellipsis 它主要用于NumPy中的矩阵切片操作.但是,您 ...

  9. Python 程序员需要知道的 30 个技巧(转载)

    Python 程序员需要知道的 30 个技巧 原创2017-04-14 伯乐专栏/阿喵 Python开发者 Python开发者 Python开发者 微信号 PythonCoder 功能介绍 人生苦短, ...

最新文章

  1. python loop call soon_从“call\u soon”回调函数执行协同路由
  2. mysql多索引结构_MySQL 索引结构
  3. Linux python impala/sasl/thrift-sasl command not found
  4. textarea中的换行符
  5. C#-自动(也叫隐式)类型转换及规则 018
  6. php5.5 连接数据库,php5.5 session_set_save_handler 连接数据库问题
  7. CCF201903-1 小中大
  8. File类的mkdir()与mkdirs()方法的区别
  9. slim.conv2d以及slim.convolution2d与tf.nn.conv2d的不同
  10. 快应用开发教程【02】--项目配置教程
  11. 详解:Java的重载方法与示例
  12. 从跑步小白到马拉松、再到百公里越野跑的晋级之路
  13. 如何开发Android手表界面ui,20款神奇的UI智能手表界面设计欣赏
  14. 熟知四种常见的BIOS信息说明【7gyy】
  15. canvas绘制火柴人
  16. 【Go语言实战】—— 时间戳转标准输出格式,标准输出转时间戳,gorm查询标准化时间
  17. 【Flutter】如何完成一个透明沉浸式状态栏
  18. houdini 函数基础
  19. 乐山市计算机学校的董事长是,乐山市计算机学校开学典礼隆重举行
  20. 小球碰壁反弹加分_js中小球碰壁反弹

热门文章

  1. Guava Cache用法介绍
  2. 正则表达式:(?=a)是什么意思?
  3. 复习支持向量机(SVM)没空看书时,掌握下面的知识就够了
  4. vue骨架屏、时间选择器、轮播图。。你想要的这里全都有
  5. JavaScript-创建日志调试对象(面向对象实例)
  6. [故障公告]14:40-15:00博客站点web服务器雪崩似的CPU 100%
  7. CFI Flash, SPI Flash, Nand Flash,Nor Flash的区别和联系
  8. java中的equals()空对象的出错
  9. 高级系统项目管理师笔记1
  10. EF6 CodeFirst+Repository+Ninject+MVC4+EasyUI实践(六)