In the process of writing your daily python program, you will have your own solution to a certain problem, or a program formula used to help debug in the process of debugging the program. Through the summary and processing of hundreds of thousands of lines of code, the editor has summed up 22 python universal formulas, which can help you solve most of the problems encountered in daily python programming. Let's take a look
foreword
In the process of writing your daily python program, you will have your own solution to a certain problem, or a program formula used to help debug in the process of debugging the program.
Through the summary and processing of hundreds of thousands of lines of code, the editor has summed up 22 python universal formulas, which can help you solve most of the problems encountered in daily python programming. Let's take a look.
1. Input multiple values at one time
The problem of numerical input is a problem often encountered in many written test questions. Entering multiple parameter values at one time can save time and code, and save time for subsequent program writing.
1 2 3 4 5 6 7 8 9 10 |
|
operation result
2. Get the index and value at the same time
When performing numerical iteration, you can use the built-in function of enumerate to obtain the numerical value of the iterable object, and at the same time obtain the index of the numerical value, and use the index to operate on the numerical value.
1 2 3 4 5 6 |
|
operation result
3. Object memory usage
Through the program in the figure below, you can query the memory usage of the object.
1 2 3 |
|
operation result
4. Query the memory address of the object
Through the built-in function id(), you can query the memory addresses of different variables
1 2 3 4 |
|
operation result
5. Check whether the list and string have the same elements
Different strings can be composed of the same letters. Similarly, lists can also be composed of the same elements. Through the following program, it can be judged whether different strings or lists have the same elements.
1 2 3 4 5 |
|
operation result
6. Merge dictionaries
When dealing with json data or the content in the database, the merging of dictionaries will be used, and sometimes dictionaries with the same key value will be encountered, which can be solved by the two methods in the program in the figure below.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
operation result
7. Check if the file exists
When the program is running, you will encounter the situation of saving some pictures and texts. At this time, you need to use the program to determine whether a certain file or folder exists.
1 2 3 4 5 6 7 |
|
operation result
8. Operate on list elements
Through the inline for loop of the Python language, the operation on all elements in the list is realized.
1 2 3 |
|
operation result
9. Convert two lists to dictionaries
Convert two lists to dictionaries, the common case is to construct a dictionary with one list as the key and the other list as the value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
operation result
10、字符串列表的排序
当大家需要对一个字符串列表进行排序时,可以利用下图中的程序进行排序。
1 2 3 |
|
运行结果
11、利用if和else对列表进行处理
利用if和else的操作,可以基于某些条件过滤数据,如下图所示。
1 2 |
|
运行结果
12、合并两个列表
对于两个列表的合并,可以通过花式的列表合并来将两个列表组合成一个新的列表。
1 2 3 4 5 |
|
运行结果
13、对字典列表进行排序
当有字典组成的列表时,可以按照字典的键值对列表进行排序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
运行结果
14、计算程序执行的时间
对于程序计算时间 的计算,可以帮助大家对于程序或者算法的性能有更好的了解。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
运行结果
15、检查是否包含子字符串
对于子字符串的检查是Python日常应用中经常遇到的一个问题,当一个字符串中包含某些关键子字符串时,将这些字符串进行打印。
1 2 3 4 5 6 |
|
运行结果
16、格式化字符串
对于Python的输入,逻辑和输出。这三个部分在编写代码时都需要某种格式,Python提供了多种格式化字符串的方法,以便获得更好和易于阅读的输出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
运行结果
17、错误捕捉
在Python语言中,提供了使用try,except和finally块处理异常报错的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
运行结果
18、列表元素频率统计
对于列表等可迭代对象中的元素进行频次的统计,也是一项非常常见的问题。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
运行结果
19、简易计算器制作
下图的程序中,不需要if-else的操作,即可制作一个简易的计算器。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
运行结果
20、链式函数调用
通过一行程序,可以调用多个不同的函数,进行计算。
1 2 3 4 5 6 7 8 9 |
|
运行结果
21、两个数值交换
Python中的交换,不仅仅可以直接通过a,b = b,a的方式进行数值的交换,而且还可以进行列表等可迭代对象的交换。
1 2 3 4 5 6 7 8 9 10 |
|
运行结果
22、检查是否有重复元素
对于检查列表中是否有重复的元素,可以通过将列表转换为set来快速检查。
1 2 3 4 |
|
运行结果
到此这篇关于22个Python的万用公式分享的文章就介绍到这了。
点击拿去
50G+学习视频教程
100+Python初阶、中阶、高阶电子书籍