admin管理员组文章数量:1794759
20条非常实用的Python代码,建议收藏!
据说Python之父-Guido Van Rossum打算让CPython更快,速度直接翻五倍,这是实实在在的好消。
Python一直以来被诟病速度慢,影响开发效率,希望这次Guido老爷子能帮python打一场漂亮的翻身仗。
这篇文章不准备介绍Python速度如何,而是给大家带来一些常用且实用的Python代码实例,几乎是开发者必备的知识点。
1、合并两个字典Python3.5之后,合并字典变得容易起来。我们可以通过**符号解压字典,并将多个字典传入{}中,实现合并。
def Merge(dict1, dict2): res = {**dict1, **dict2} return res # 两个字典 dict1 = {"name": "Joy", "age": 25} dict2 = {"name": "Joy", "city": "New York"} dict3 = Merge(dict1, dict2) print(dict3)输出:
{'name': 'Joy', 'age': 25, 'city': 'New York'} 2、链式比较python有链式比较的机制,在一行里支持多种运算符比较。相当于拆分多个逻辑表达式,再进行逻辑与操作。
a = 5 print(2 < a < 8) print(1 == a < 3)输出:
True False 3、重复打印字符串将一个字符串重复打印多次,一般使用循环实现,但有更简易的方式可以实现。
n = 5 string = "Hello!" print(string * n)输出:
Hello!Hello!Hello!Hello!Hello! 4、检查文件是否存在我们知道Python有专门处理系统交互的模块-os,它可以处理文件的各种增删改查操作。
那如何检查一个文件是否存在呢?os模块可以轻松实现。
from os import path def check_for_file(): print("Does file exist:", path.exists("data.csv")) if __name__=="__main__": check_for_file()输出:
Does file exist: False 5、检索列表最后一个元素在使用列表的时候,有时会需要取最后一个元素,有下面几种方式可以实现。
my_list = ['banana', 'apple', 'orange', 'pineapple'] #索引方法 last_element = my_list[-1] #pop方法 last_element = my_list.pop()输出:
'pineapple' 6、列表推导式列表推导式是for循环的简易形式,可以在一行代码里创建一个新列表,同时能通过if语句进行判断筛选
def get_vowels(string): return [vowel for vowel in string if vowel in 'aeiou'] print("Vowels are:", get_vowels('This is some random string'))输出:
Vowels are: ['i', 'i', 'o', 'e', 'a', 'o', 'i'] 7、计算代码执行时间python中time模块提供了时间处理相关的各种函数方法,我们可以使用它来计算代码执行的时间。
import time start_time = time.time() total = 0 for i in range(10): total += i print("Sum:", total) end_time = time.time() time_taken = end_time - start_time print("Time: ", time_taken)输出:
Sum: 45 Time: 0.0009975433349609375 8、查找出现次数最多的元素使用max方法找出列表中出现次数最多的元素。
def most_frequent(list): return max(set(list), key=list.count) mylist = [1,1,2,3,4,5,6,6,2,2] print("出现次数最多的元素是:", most_frequent(mylist))输出:
出现次数最多的元素是: 2 9、将两个列表转换为字典有两个列表,将列表A里的元素作为键,将列表B里的对应元素作为值,组成一个字典。
def list_to_dictionary(keys, values): return dict(zip(keys, values)) list1 = [1, 2, 3] list2 = ['one', 'two', 'three'] print(list_to_dictionary(list1, list2))输出:
{1: 'one', 2: 'two', 3: 'three'} 10、异常处理Python提供了try...except...finally的方式来处理代码异常,当然还有其他组合的方式。
a, b = 1,0 try: print(a/b) except ZeroDivisionError: print("Can not divide by zero") finally: print("Executing finally block")输出:
Can not divide by zero Executing finally block 11、反转字符串使用切片操作对字符串进行反转,这是比较直接有效的方式。这也可以用来检测回文数。
str = "Hello World" print("反转后字符串是:", str[::-1])输出:
反转后字符串是: dlroW olleH 12、字符串列表组成单个字符串使用join方法将字符串列表组成单个字符串。
list = ["Hello", "world", "Ok", "Bye!"] combined_string = " ".join(list) print(combined_string)输出:
Hello world Ok Bye! 13、返回字典缺失键的默认值字典中的get方法用于返回指定键的值,如果键不在字典中返回默认值 None 或者设置的默认值。
dict = {1:'one', 2:'two', 4:'four'} #returning three as default value print(dict.get(3, 'three')) print("原始字典:", dict)输出:
three 原始字典: {1: 'one', 2: 'two', 4: 'four'} 14、交换两个变量的值在不使用临时变量的前提下,交换两个变量的值。
a, b = 5, 10 # 方法1 a, b = b, a # 方法2 def swap(a,b): return b,a swap(a,b) 15、正则表达式正则表达式用来匹配处理字符串,python中的re模块提供了全部的正则功能。
import re text = "The rain in spain" result = re.search("rain", text) print(True if result else False)输出:
True 16、筛选值python中的filter方法可以用来进行值的筛选。
my_list = [0,1,2,3,6,7,9,11] result = filter(lambda x: x % 2!=0, my_list) print(list(result))输出:
[1, 3, 7, 9, 11] 17、统计字频判断字符串每个元素出现的次数,可以用collections模块中的Counter方法来实现,非常简洁。
from collections import Counter result = Counter('banana') print(result)输出:
Counter({'a': 3, 'n': 2, 'b': 1}) 18、变量的内存占用如何输出python中变量的内存占用大小,可以通过sys模块来实现。
import sys var1 = 15 list1 = [1,2,3,4,5] print(sys.getsizeof(var1)) print(sys.getsizeof(list1))输出:
28 104 19、链式函数调用在一行代码中调用多个函数。
def add(a, b): return a + b def subtract(a, b): return a - b a, b = 5, 10 print((add if b > a else subtract)(a,b))输出:
15 20、从列表中删除重复项删除列表中重复项一般可以通过遍历来筛选去重,或者直接使用集合方法。
list1 = [1,2,3,3,4,'John', 'Ana', 'Mark', 'John'] # 方法1 def remove_duplicate(list_value): return list(set(list_value)) print(remove_duplicate(list1)) # 方法2 result = [] [result.append(x) for x in list1 if x not in result] print(result)输出:
[1, 2, 3, 4, 'Ana', 'John', 'Mark'] [1, 2, 3, 4, 'John', 'Ana', 'Mark'] 推荐阅读: 入门: 最全的零基础学Python的问题 | 零基础学了8个月的Python | 实战项目 |学Python就是这条捷径 干货:爬取豆瓣短评,电影《后来的我们》 | 38年NBA最佳球员分析 | 从万众期待到口碑扑街!唐探3令人失望 | 笑看新倚天屠龙记 | 灯谜答题王 |用Python做个海量小姐姐素描图 |碟中谍这么火,我用机器学习做个迷你推荐系统电影 趣味:弹球游戏 | 九宫格 | 漂亮的花 | 两百行Python《天天酷跑》游戏! AI: 会做诗的机器人 | 给图片上色 | 预测收入 | 碟中谍这么火,我用机器学习做个迷你推荐系统电影 小工具: Pdf转Word,轻松搞定表格和水印! | 一键把html网页保存为pdf!| 再见PDF提取收费! | 用90行代码打造最强PDF转换器,word、PPT、excel、markdown、html一键转换 | 制作一款钉钉低价机票提示器! |60行代码做了一个语音壁纸切换器天天看小姐姐!|年度爆款文案
1).卧槽!Pdf转Word用Python轻松搞定!
2).学Python真香!我用100行代码做了个网站,帮人PS旅行图片,赚个鸡腿吃
3).首播过亿,火爆全网,我分析了《乘风破浪的姐姐》,发现了这些秘密
4).80行代码!用Python做一个哆来A梦分身
5).你必须掌握的20个python代码,短小精悍,用处无穷
6).30个Python奇淫技巧集
7).我总结的80页《菜鸟学Python精选干货.pdf》,都是干货
8).再见Python!我要学Go了!2500字深度分析!
9).发现一个舔狗福利!这个Python爬虫神器太爽了,自动下载妹子图片
版权声明:本文标题:20条非常实用的Python代码,建议收藏! 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686584672a84541.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论