I am an engineer by education. I learned Fortran and MATLAB in college to solve engineering questions like energy and mass transfer, but I don’t have a solid software engineering training. When I started to learn Python for data science and machine learning recently, I did it by kind of ‘result-oriented’ way, which made my code a little bit messy and inefficient.
我是受过教育的工程师。 我在大学期间学习了Fortran和MATLAB,以解决诸如能量和质量传递的工程问题,但是我没有扎实的软件工程培训。 最近,当我开始学习用于数据科学和机器学习的Python时,我是以“面向结果”的方式完成的,这使我的代码有些混乱且效率低下。
Through my self-learning journey, I find several Python functions that improve my code a lot. I wish to share 7 Python tricks in this post that I wish I had known earlier as a beginner.
在我的自学过程中,我发现了一些Python函数,这些函数极大地改善了我的代码。 我希望在这篇文章中分享7个Python技巧,我希望我以前是一个初学者。
1.清单理解 (1. list comprehension)
List comprehension saves time and codes. It is a simple way to generate a list comparing to using loops.
列表理解可以节省时间和代码。 与使用循环相比,这是一种生成列表的简单方法。
Let’s look at an example: we have a list: x, containing 5 integers, and we want to make a list,x_square, that contains the square of the 5 integers in x.
让我们看一个例子:我们有一个列表:x,包含5个整数,我们要创建一个列表,x_square,包含x中5个整数的平方。
A for loop method will be like this:
for循环方法将如下所示:

List comprehension can do the same job in one line of code:
列表理解可以在一行代码中完成相同的工作:

It can also include a condition, say we only want a list containing the square of integers only for the integers greater than 2. For the loop method, we can add an if statement:
它也可以包含一个条件,例如,我们只想要一个包含整数平方的列表,仅针对大于2的整数。对于循环方法,我们可以添加一个if语句:

List comprehension: add the condition in the end — still one line of code
列表理解:最后添加条件-仍然是一行代码

2. lambda函数(2. lambda function)
lambda
is a small anonymous function that behaves like a normal function.
lambda
是一个小的匿名函数,其行为类似于普通函数。
For example, a function calculates the square can be defined:
例如,可以定义一个计算平方的函数:
def cal_square(x):
returm x**2
Using a lambda
function, it can be done as
使用lambda
函数,可以完成
cal_square = lambda(x:x**2)
It is very convenient to sort thing using your own defined function, for example:
使用自己定义的函数对事物进行排序非常方便,例如:

Also very useful to manipulate pandas dataframe using apply
:
使用apply
操作pandas数据框也非常有用:

3. map() (3. map())
map
take a function and a list as parameters, it applies the function to every item in the list. It is really handy because it avoids using loops, and it is faster.
map
将一个函数和一个列表作为参数,它将函数应用于列表中的每个项目。 它非常方便,因为它避免使用循环,并且速度更快。
Take a simple function, for example, if we want to calculate the square of every item in the list, we can define a function to do the calculation and apply the function to the list using map()
以一个简单的函数为例,如果我们要计算列表中每个项目的平方,我们可以定义一个函数进行计算,然后使用map()
将函数应用于列表

4. filter()(4. filter())
As suggested by the name, filter
apply a filter to a list and return items that satisfied the conditions. If we want only the positive items in a list, we can define a filter function and then apply filter
to the list.
顾名思义, filter
将过滤filter
应用于列表并返回满足条件的项目。 如果只需要列表中的肯定项,则可以定义过滤器功能,然后将filter
应用于列表。

You can also use lambda
here to make it simpler.
您也可以在此处使用lambda
使其更简单。

5. join() (5. join())
join
is a way to concatenate strings together. The straightforward way to join strings is to use +
, for example:
join
是将字符串连接在一起的一种方式。 连接字符串的直接方法是使用+
,例如:

this can be done by using join
easier:
这可以通过使用join
更容易做到:

6.枚举() (6. enumerate())
enumerate
adds counters to the data. For example:
enumerate
将计数器添加到数据。 例如:

It is very useful to use in a loop:
在循环中使用非常有用:

7. format() (7. format())
format()
is a string formatting method in Python3. It is convenient to generate or print strings with multiple variables.
format()
是Python3中的字符串格式化方法。 生成或打印具有多个变量的字符串很方便。
For example:
例如:

It is quite useful in loops:
它在循环中非常有用:

You can also play around with the number using .5f
:
您也可以使用.5f
来处理.5f
:

F-string is another way of playing around with string and it is even smarter and avoids a long line.
F弦是另一种与弦打交道的方式,它甚至更聪明并且避免了排长队。

That’s it. These tricks make my code a lot simpler and better without writing unnecessary loops. I hope these tricks help you too.
而已。 这些技巧使我的代码更简单,更好,而无需编写不必要的循环。 我希望这些技巧也能对您有所帮助。
Thanks for reading, happy coding.
感谢您阅读,编码愉快。
I also wrote articles about Numpy and Pandas, you can take a look if you are interested!
我还写了有关Numpy和Pandas的文章,如果有兴趣可以看看!
翻译自: https://towardsdatascience.com/7-python-tricks-to-make-my-code-better-and-smarter-60dfde0b6c49