python 每天触发_每天都有Python

python 每天触发

Python is one of the languages that beginners find very easy to start with. It is an interpreted, object-oriented and high-level programming language.

Python是初学者发现非常容易入门的语言之一。 它是一种解释型,面向对象的高级编程语言。

In this post, we will cover interview questions, python tricks to perform an operation, how to optimize the code and many more.

在这篇文章中,我们将介绍访谈问题,执行操作的python技巧,如何优化代码等等。

So, without further ado, let’s get started:

因此,事不宜迟,让我们开始吧

1)优化和分析: (1) Optimization and Profiling:)

I have encountered this question in interviews a lot — how do you optimize the python code. Well, I generally prefer to build the data processing pipelines early on by relying on standard best practices (which I will discuss later in the post). Then, start identifying the inefficiencies that might exist in following forms:

我在访谈中经常遇到这个问题-您如何优化python代码。 好吧,我通常更喜欢依靠标准最佳实践尽早构建数据处理管道(我将在后面的文章中进行讨论)。 然后,开始确定以下形式可能存在的效率低下:

  • A single statement — timeit (it is demonstrated in multiple examples towards end)

    一条语句-timeit (将在结尾的多个示例中进行演示)

  • A certain section of the code — cProfile

    代码的特定部分— cProfile

Lets learn code profiling first.

让我们先学习代码分析。

What is a profile? As per python docs:

什么是个人资料? 根据python docs

A profile is a set of statistics that describes how often and for how long various parts of the program executed.

概要文件是一组统计信息,描述了程序的各个部分执行的频率和时间。

We will see how the profile is evaluated for a given python function using standard library — cProfile.

我们将看到如何使用标准库cProfile对给定的python函数评估配置文件。

Lets take an example of Fibonacci series to illustrate how the profile looks like:

让我们以斐波那契数列为例来说明配置文件的外观:

Image for post
Fibonacci series profiling
斐波那契系列分析
Image for post
Profile Output
配置文件输出

The first line in the output tells how many total calls were monitored, out of which how many were primitive. This indicates the existence of recursion in the code( as only 4 calls were primitive out of 180 calls).

输出的第一行告诉您监视了总数为多少的呼叫,其中原始数目为多少。 这表明代码中存在递归(因为180个调用中只有4个是原始的)。

PyCallGraph is another way to profile the code and is easier to interpret as the output is in the form of a png graph image. We will not discuss much here as it is beyond the scope of this post, more on this can be read here.

PyCallGraph是分析代码的另一种方法,并且由于输出为png图形图像,因此更易于解释。 我们不会在这里讨论太多,因为这超出了本文的范围,有关更多信息,请参见此处

2) 使用装饰器缓存结果: (2) Results caching with decorator:)

Continuing with the discussion of optimization from above, it requires the analysis of the run time and memory profile of the code. If the code has longer run time and heavy memory usage, it is termed as expensive.

从上面继续进行优化的讨论,需要分析代码的运行时间和内存配置文件。 如果代码具有更长的运行时间和大量的内存使用,则称为昂贵。

The application is then analyzed to identify the expensive code which has scope for optimization. The typical way to speed up the code is to cache the culprit (yes !!!, for us it is).

然后对应用程序进行分析,以确定具有优化范围的昂贵代码。 加快代码速度的典型方法是缓存罪魁祸首(是的,对我们来说是这样)。

Caching serves the purpose of storing the results which are computationally expensive. So, they are just computed once, cached and retrieved via wrappers called as Decorators.

高速缓存用于存储计算上昂贵的结果。 因此,它们只计算一次,就通过称为Decorators的包装器进行缓存和检索。

A decorator is a function whose input and output is also a function.

装饰器是一个函数,其输入和输出也是一个函数。

Image for post
Decorator Fibonacci Example
装饰器斐波那契示例

3) Join()组合字符串: (3) Join() to combine strings:)

Strings are immutable. This implies that every time 2 strings are concatenated; a new representation is created as they can’t be modified in place. Also note that the time measurement of string concatenation is a function of the number of concatenation operations and the average length of strings.

字符串是不可变的。 这意味着每次将2个字符串连接起来; 由于无法修改它们,因此创建了一个新的表示形式。 还要注意,字符串连接的时间度量是连接操作数和字符串平均长度的函数。

Use ‘Join’ to concatenate the strings as it is faster than ‘+’ operator

使用“联接”来连接字符串,因为它比“ +”运算符要快

Image for post
String Concatenation
字符串串联

4)柜台 (4) Counter)

A ‘+’ operator does not work if you want to combine two dictionaries by adding the values for common keys.

如果要通过添加公用键的值来组合两个字典,则“ +”运算符不起作用。

For e.g. A+B throws error in below code, but can be achieved using Counter:

例如,A + B在下面的代码中引发错误,但是可以使用Counter来实现:

Image for post
Counter to combine 2 dictionaries
计数器合并2个词典

Similarly, we can find the intersection and union of A and B as well.

同样,我们也可以找到A和B的交集和并集。

Another very basic application of using ‘Counter’ is when we pass a string as input to count the occurrence of each alphabet in the string, for e.g. ‘python everyday’:

使用“计数器”的另一个非常基本的应用是,当我们传递一个字符串作为输入来计算字符串中每个字母的出现时,例如“每天使用python”:

Image for post
String count
字符串数

The count of each alphabet to appear in the same order as it is appearing in the dictionary can be achieved by using OrderedDict:

可以使用OrderedDict来获得每个字母的顺序与出现在字典中的顺序相同:

Image for post
OrderedDict
OrderedDict

Counter can also be used to count the occurrence of each element in a dictionary by flattening it:

计数器也可以通过展平它来计算字典中每个元素的出现:

Image for post
Counting the elements after flattening the dictionary values
展平字典值后计算元素

5)变量交换 (5) Variable Swapping)

Avoid creating temporary variable for swapping the variables. To understand it via an example, let’s see how the swapping happens for the expression —

避免创建用于交换变量的临时变量。 为了通过一个例子来理解它,让我们看一下表达式的交换是如何发生的-

x, y = y, x

x,y = y,x

Step 1: The right-hand side y, x is evaluated first, which created a 2-element tuple in the memory. The 2 elements are the objects identified as y and x for Python to address it internally.

步骤1:首先评估右侧y,x,这在内存中创建了一个2元素元组。 这2个元素是被y和x标识的对象,以便Python在内部对其进行寻址。

Step 2: During evaluation of left-hand side expression, the tuple is assigned to the left-hand side and gets unpacked into x first and then y

步骤2:在评估左侧表达式时,将元组分配给左侧,然后先将其解压缩为x,然后将其解压缩为y

Image for post
Variable Swapping
可变交换

6) 清单与双端队列: (6) List vs deque:)

Deque is the double ended queue which supports memory efficient appends and pops from both sides.

双端队列是双端队列支持从两侧存储器高效追加和弹出。

List: Insertion at the end of a list is efficient, however, when a new element is inserted at the front of the list, all subsequent elements indexing shifts by 1

列表:列表的末尾插入是有效的,但是,当在列表的前面插入新元素时,所有后续元素的索引移位1

Here deque comes handy which is faster, being a double-linked list.

这是双链表,双端队列比较方便,速度更快。

Image for post
List vs deque
列表与双端队列

7)“ in”关键字 (7) ‘in’ keyword)

‘in’ keyword is the fastest and cleanest way to check the existence of an element.

“ in”关键字是检查元素是否存在的最快,最干净的方法。

Image for post
‘in’ vs ‘get’
“进”与“得到”

8)映射,缩小和过滤关键字 (8) Map, reduce, and filter keywords)

Filter outputs a new list containing the elements that satisfy a given condition.

过滤器输出一个新列表,其中包含满足给定条件的元素。

Map object is used to cast the given function, in our example below, an exponential function, to each item of a specified iterable.

Map对象用于将给定函数(在下面的示例中)转换为指数函数到指定可迭代项的每个项目。

Reduce returns a single value by applying the function to an iterable. Its labelled as reduce as it reduces the elements in an iterable to a single cumulative value

通过将函数应用于可迭代对象, Reduce返回单个值。 它被标记为reduce,因为它将可迭代的元素减少到单个累积值

Image for post
Map, reduce and filter
映射,缩小和过滤

9) 发电机的使用: (9) Use of generators:)

To understand generators, lets first understand iterables. For e.g., reading the elements of a list one by one is called as iteration, where the list is an iterable. But the issue with iterable is that it stores all the values in memory which may not be required all the time, hence comes the role of generators.

要了解生成器,首先要了解可迭代项。 例如,一张一张地读取列表的元素称为迭代,其中列表是可迭代的。 但是,可迭代的问题是它将所有值存储在内存中,而这些值可能并非一直都需要,因此产生器的作用越来越大。

Generator is a sub-type of iterator which generates the values on the fly. This results in saving memory and improving performance.

生成器是迭代器的子类型,它可以即时生成值。 这样可以节省内存并提高性能。

For e.g. in case of video streaming to keep a tab on burglary, one is not interested in each and every image but only the one where potential burglary can happen. So, it is like on ongoing process where only a certain event related information needs analysis and not entire input needs to be stored in advance.

例如,在视频流媒体上保持抢劫状态的情况下,对每个图像都不感兴趣,而仅对可能发生盗窃的图像感兴趣。 因此,就像正在进行的过程一样,其中仅需要分析与特定事件相关的信息,而无需预先存储所有输入。

It might also be the case that we initially generate a list of numbers but based on the algorithm, only a few elements are used. This led to wastage of CPU cycles in generating those numbers. Not just this, a lot of memory is used to store those numbers in the objects concurrently.

也可能是这样的情况,我们最初会生成一个数字列表,但是基于该算法,仅使用了一些元素。 这导致浪费了CPU周期来生成这些数字。 不仅如此,大量内存用于同时将这些数字存储在对象中。

Image for post

More pointers on improving the code efficiency which are self explanatory:

关于提高代码效率的更多说明不言而喻:

  • Python is faster retrieving a local variable than retrieving a global variable. Accessing global objects runs through a full look up every time the object is needed, thereby making it slower.

    Python检索局部变量比检索全局变量更快 。 每次需要对象时,都会通过全面查找来访问全局对象,从而使其变慢。

  • List comprehension is faster because it doesn’t need to load the append and call it as a function. Look up, load and function call is time taking process which adds up over iterations.

    列表理解速度更快,因为它不需要加载附加并将其作为函数调用。 查找,加载和函数调用是耗时的过程,需要反复进行累加。

  • Try except over ‘if else’

    尝试除“其他”外

  • Intersection vs ‘&’ operator:

    交叉点与“&”运算符:

Image for post
‘&’ over ‘Intersection’
'&'超过'Intersection'
  • Unpacking Operator:

    开箱操作员:

Image for post
Use of * as unpacking operator
使用*作为开箱算子

And, there are many more. As they say, learning is never ending. I will keep writing more on Python and its magnanimity.

而且,还有更多。 正如他们所说,学习永无止境。 我将继续写更多关于Python及其海量性的文章。

Till then, continue your journey of becoming an avid ‘Pythonista’.

直到那时,继续您成为狂热的“ Pythonista”的旅程。

PS: I have added snippets of python code to illustrate the concepts, instead of creating multiple gists. Would be great to know if creating multiple smaller gists for explaining each concept is the only way to demonstrate or there is a quicker workaround.

PS:我添加了python代码片段来说明概念,而不是创建多个要点。 非常想知道是否创建多个较小的要点来解释每个概念是唯一的证明方法,还是有更快的解决方法。

The entire code is placed in jupyter notebook here.

整个代码放在这里的 jupyter笔记本中。

Thanks for reading.

谢谢阅读。

翻译自: https://towardsdatascience.com/python-everyday-fa8946524532

python 每天触发

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值