Python 中的 Pygorithm 模块
Pygorithm 模块是一个教育模块,包含各种算法的实现。此模块的最佳用途是从实现的算法中获取用 Python 编写的代码。但它也可以用于实际编程,我们可以在给定的数据集中应用各种算法。
查找数据结构
在 Python 环境中安装模块后,我们可以在包中找到各种数据结构。
示例
from pygorithm import data_structures help(data_structures
运行上述代码,我们会得到以下结果 -
输出
Help on package pygorithm.data_structures in pygorithm: NAME pygorithm.data_structures - Collection of data structure examples PACKAGE CONTENTS graph heap linked_list quadtree queue stack tree trie DATA __all__ = ['graph', 'heap', 'linked_list', 'queue', 'stack', 'tree', '...
获取算法代码
在下面的程序中,我们看到了如何获取队列数据结构算法的代码。
示例
from pygorithm.data_structures.queue import Queue the_Queue = Queue() print(the_Queue.get_code())
运行上述代码,我们会得到以下结果 -
输出
class Queue(object): """Queue Queue implementation """ def __init__(self, limit=10): """ :param limit: Queue limit size, default @ 10 """ self.queue = [] self.front = None self.rear = None self.limit = limit self.size = 0 ………………………… ………………
应用排序
在以下示例中,我们看到了如何对给定的列表应用快速排序。
示例
from pygorithm.sorting import quick_sort my_list = [3,9,5,21,2,43,18] sorted_list = quick_sort.sort(my_list) print(sorted_list)
运行上述代码,我们会得到以下结果 -
输出
[2, 3, 5, 9, 18, 21, 43]
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP