Python package structure and method of the good, why did you have to learn the data structure?

Foreword

In front of everyone learned the basics of Python all know, our Python package the lists, dictionaries and other advanced data types, and they are presented with a series of add, delete, change, in addition to the method, so that we can deal with some problems easily. Those of us who are at the current level of technology might think these things enough, still be able to quickly solve a lot of problems. But with the depth of knowledge, with the problem of constantly becomes difficult, many times we go with lists, dictionaries, these advanced data types to solve the problem, then may have seemed a bit powerless. ** no useless knowledge in the world, and no useless man! ** In fact, you will find that by following in-depth study,Data Structure This course is independent of language, Though he is what language, which round after round are the same, as for the Python language to achieve this, then, whether he would be simpler, it remains to be our go-depth study and understanding.

Built-in data types Python Performance Analysis

Below we will combine the timeit Python module to analyze in depth, built-in Python lists, dictionaries and other types of performance data, which you may have in-depth study of data structures stronger expectations.

timeit module

timeit module can be used to test a small Python code execution speed.
Python is timeit module defines Timer class accepts two parameters. Two parameters are strings, the first parameter you to be timed statement or function, the second argument is the import statement environment for the first argument, the first argument is the place where, generally import statement "from __ main __ inport ..." .

class timeit.Timer(stmt='pass', setup='pass', timer=<timer function>)

To summarize:
the Timer is a measure of small pieces of code execution speed class.
stmt parameter is to be tested code statement (Statment);
Setup parameters is required when running the code set;
the Timer parameter is a timer function, and platform-dependent.
timeit.Timer.timeit (= Number 1000000)
the Timer class object execution speed test method statement. The number argument is the number of tests when the test code, the default is 1 million times. Method returns the average time to execute code, a type of floating-point number of seconds.

A list of built-in method performance analysis

Use the following timeit module Timer class to experiment with various methods list the type of run time to find out the lowest time complexity of the method:

from timeit import Timer
def t1():
    li = []
    for i in range(10000):
        li.append(i)

def t2():
    li = []
    for i in range(10000):
        li = li + [i]
        # li += [i]

def t3():
    li = [i for i in range(10000)]

def t4():
    li = list(range(10000))

def t5():
    li = []
    for i in range(10000):
        li.extend([i])

def t7():
    li = []
    for i in range(10000):
        li.insert(0, i)


timer1 = Timer("t1()", "from __main__ import t1")
print("append:", timer1.timeit(1000))

timer2 = Timer("t2()", "from __main__ import t2")
print("+:", timer2.timeit(1000))

timer3 = Timer("t3()", "from __main__ import t3")
print("[i for i in range]:", timer3.timeit(1000))

timer4 = Timer("t4()", "from __main__ import t4")
print("list(range()):", timer4.timeit(1000))

timer5 = Timer("t5()", "from __main__ import t5")
print("extend:", timer5.timeit(1000))

timer6 = Timer("t6()", "from __main__ import t6")
print("append", timer6.timeit(1000))

timer7 = Timer("t7()", "from __main__ import t7")
print("insert(0)", timer7.timeit(1000))

Here Insert Picture Description
By the above test can be found using the time complexity code list generator is relatively low, wherein the time of the operation + high complexity, because in doing + operation, each of the two list elements is generated by adding a third a good list to store the sum of list elements, plus one generates a list in memory, so down will cause a lot of memory is not released, seriously take up memory space, and the operation of each step is very time-consuming.

List of built-operation time complexity

List of time complexity of built-in actions shown below:
Here Insert Picture Description

Dictionary built-operating time complexity

List of time complexity of built-in actions shown below:
Here Insert Picture Description

At last

Some code information and pictures from my study. Talking about this and perhaps we still do not understand why we have to study the data structure, more advanced data structure a series of questions such as what are the benefits.

To solve the problem we need to save the data in, and then based on how data is stored to design algorithms for processing, then the different data storage needs will lead to different algorithms for processing. We hope that the efficiency of the algorithm to solve the problem as quickly as possible, so we need to consider how data is actually stored problem, which is the data structure.

In fact, see here, the list itself is a dictionary data structure, Python is just a good package, and other types of advanced data structures you need to realize ourselves, our process is to achieve a deeper understanding of computer knowledge!

Published 17 original articles · won praise 94 · views 7979

Guess you like

Origin blog.csdn.net/qq_43779324/article/details/104951627