如何用Python 实现自动排班

Python019

如何用Python 实现自动排班,第1张

pyexcel-xls is a tiny wrapper library to read, manipulate and write data in xls format and it can read xlsx and xlsm fromat. You are likely to use it with pyexcel.

Known constraints

Fonts, colors and charts are not supported.

Installation

You can install it via pip:

$ pip install pyexcel-xls

or clone it and install it:

$ git clone hexcel-xls.git$ cd pyexcel-xls$ python setup.py install

Usage

As a standalone library

Write to an xls file

Here’s the sample code to write a dictionary to an xls file:

>>>from pyexcel_xls import save_data>>>data = OrderedDict() # from collections import OrderedDict>>>data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})>>>data.update({"Sheet 2": [["row 1", "row 2", "row 3"]]})>>>save_data("your_file.xls", data)

Read from an xls file

Here’s the sample code:

>>>from pyexcel_xls import get_data>>>data = get_data("your_file.xls")>>>import json>>>print(json.dumps(data)){"Sheet 1": [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], "Sheet 2": [["row 1", "row 2", "row 3"]]}

Write an xls to memory

Here’s the sample code to write a dictionary to an xls file:

>>>from pyexcel_xls import save_data>>>data = OrderedDict()>>>data.update({"Sheet 1": [[1, 2, 3], [4, 5, 6]]})>>>data.update({"Sheet 2": [[7, 8, 9], [10, 11, 12]]})>>>io = StringIO()>>>save_data(io, data)>>># do something with the io>>># In reality, you might give it to your http response>>># object for downloading

Read from an xls from memory

Continue from previous example:

>>># This is just an illustration>>># In reality, you might deal with xls file upload>>># where you will read from requests.FILES['YOUR_XL_FILE']>>>data = get_data(io)>>>print(json.dumps(data)){"Sheet 1": [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], "Sheet 2": [[7.0, 8.0, 9.0], [10.0, 11.0, 12.0]]}

As a pyexcel plugin

Import it in your file to enable this plugin:

from pyexcel.ext import xls

Please note only pyexcel version 0.0.4+ support this.

Reading from an xls file

Here is the sample code:

>>>import pyexcel as pe>>>from pyexcel.ext import xls>>>sheet = pe.get_book(file_name="your_file.xls")>>>sheetSheet Name: Sheet 1+---+---+---+| 1 | 2 | 3 |+---+---+---+| 4 | 5 | 6 |+---+---+---+Sheet Name: Sheet 2+-------+-------+-------+| row 1 | row 2 | row 3 |+-------+-------+-------+

Writing to an xls file

Here is the sample code:

>>>sheet.save_as("another_file.xls")

Reading from a IO instance

You got to wrap the binary content with stream to get xls working:

>>># This is just an illustration>>># In reality, you might deal with xls file upload>>># where you will read from requests.FILES['YOUR_XLS_FILE']>>>xlsfile = "another_file.xls">>>with open(xlsfile, "rb") as f:...     content = f.read()...     r = pe.get_book(file_type="xls", file_content=content)...     print(r)...Sheet Name: Sheet 1+---+---+---+| 1 | 2 | 3 |+---+---+---+| 4 | 5 | 6 |+---+---+---+Sheet Name: Sheet 2+-------+-------+-------+| row 1 | row 2 | row 3 |+-------+-------+-------+

Writing to a StringIO instance

You need to pass a StringIO instance to Writer:

>>>data = [...     [1, 2, 3],...     [4, 5, 6]... ]>>>io = StringIO()>>>sheet = pe.Sheet(data)>>>sheet.save_to_memory("xls", io)>>># then do something with io>>># In reality, you might give it to your http response>>># object for downloading

License

New BSD License

Known Issues

If a zero was typed in a DATE formatted field in xls, you will get “01/01/1900”.

If a zero was typed in a TIME formatted field in xls, you will get “00:00:00”.

Dependencies

xlrd

xlwt-future

pyexcel-io >= 0.0.4

class SortMethod:''' 插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的、个数加一的有序数据,算法适用于少量数据的排序,时间复杂度为O(n^2)。是稳定的排序方法。插入算法把要排序的数组分成两部分:第一部分包含了这个数组的所有元素,但将最后一个元素除外(让数组多一个空间才有插入的位置)第二部分就只包含这一个元素(即待插入元素)。在第一部分排序完成后,再将这个最后元素插入到已排好序的第一部分中。'''def insert_sort(lists):# 插入排序count = len(lists)for i in range(1, count):key = lists[i]j = i - 1while j >= 0:if lists[j] >key:lists[j + 1] = lists[j]lists[j] = keyj -= 1return lists''' 希尔排序 (Shell Sort) 是插入排序的一种。也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本。希尔排序是非稳定排序算法。该方法因 DL.Shell 于 1959 年提出而得名。希尔排序是把记录按下标的一定增量分组,对每组使用直接插入排序算法排序;随着增量逐渐减少,每组包含的关键词越来越多,当增量减至 1 时,整个文件恰被分成一组,算法便终止。'''def shell_sort(lists):# 希尔排序count = len(lists)step = 2group = count / stepwhile group >0:for i in range(0, group):j = i + groupwhile j <count:k = j - groupkey = lists[j]while k >= 0:if lists[k] >key:lists[k + group] = lists[k]lists[k] = keyk -= groupj += groupgroup /= stepreturn lists''' 冒泡排序重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。'''def bubble_sort(lists):# 冒泡排序count = len(lists)for i in range(0, count):for j in range(i + 1, count):if lists[i] >lists[j]:temp = lists[j]lists[j] = lists[i]lists[i] = tempreturn lists'''快速排序通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列'''def quick_sort(lists, left, right):# 快速排序if left >= right:return listskey = lists[left]low = lefthigh = rightwhile left <right:while left <right and lists[right] >= key:right -= 1lists[left] = lists[right]while left <right and lists[left] <= key:left += 1lists[right] = lists[left]lists[right] = keyquick_sort(lists, low, left - 1)quick_sort(lists, left + 1, high)return lists'''直接选择排序第 1 趟,在待排序记录 r[1] ~ r[n] 中选出最小的记录,将它与 r[1] 交换;第 2 趟,在待排序记录 r[2] ~ r[n] 中选出最小的记录,将它与 r[2] 交换;以此类推,第 i 趟在待排序记录 r[i] ~ r[n] 中选出最小的记录,将它与 r[i] 交换,使有序序列不断增长直到全部排序完毕。'''def select_sort(lists):# 选择排序count = len(lists)for i in range(0, count):min = ifor j in range(i + 1, count):if lists[min] >lists[j]:min = jtemp = lists[min]lists[min] = lists[i]lists[i] = tempreturn lists''' 堆排序 (Heapsort) 是指利用堆积树(堆)这种数据结构所设计的一种排序算法,它是选择排序的一种。可以利用数组的特点快速定位指定索引的元素。堆分为大根堆和小根堆,是完全二叉树。大根堆的要求是每个节点的值都不大于其父节点的值,即 A[PARENT[i]] >= A[i]。在数组的非降序排序中,需要使用的就是大根堆,因为根据大根堆的要求可知,最大的值一定在堆顶。'''# 调整堆def adjust_heap(lists, i, size):lchild = 2 * i + 1rchild = 2 * i + 2max = iif i <size / 2:if lchild <size and lists[lchild] >lists[max]:max = lchildif rchild <size and lists[rchild] >lists[max]:max = rchildif max != i:lists[max], lists[i] = lists[i], lists[max]adjust_heap(lists, max, size)# 创建堆def build_heap(lists, size):for i in range(0, (size/2))[::-1]:adjust_heap(lists, i, size)# 堆排序def heap_sort(lists):size = len(lists)build_heap(lists, size)for i in range(0, size)[::-1]:lists[0], lists[i] = lists[i], lists[0]adjust_heap(lists, 0, i)''' 归并排序是建立在归并操作上的一种有效的排序算法,该算法是采用分治法 (Divide and Conquer) 的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为二路归并。归并过程为:比较 a[i] 和 a[j] 的大小,若 a[i]≤a[j],则将第一个有序表中的元素 a[i] 复制到 r[k] 中,并令 i 和 k 分别加上 1;否则将第二个有序表中的元素 a[j] 复制到 r[k] 中,并令 j 和 k 分别加上 1,如此循环下去,直到其中一个有序表取完,然后再将另一个有序表中剩余的元素复制到 r 中从下标 k 到下标 t 的单元。归并排序的算法我们通常用递归实现,先把待排序区间 [s,t] 以中点二分,接着把左边子区间排序,再把右边子区间排序,最后把左区间和右区间用一次归并操作合并成有序的区间 [s,t]。'''def merge(left, right):i, j = 0, 0result = []while i <len(left) and j <len(right):if left[i] <= right[j]:result.append(left[i])i += 1else:result.append(right[j])j += 1result += left[i:]result += right[j:]return resultdef merge_sort(lists):# 归并排序if len(lists) <= 1:return listsnum = len(lists) / 2left = merge_sort(lists[:num])right = merge_sort(lists[num:])return merge(left, right)''' 基数排序 (radix sort) 属于“分配式排序” (distribution sort),又称“桶子法” (bucket sort) 或 bin sort,顾名思义,它是透过键值的部份资讯,将要排序的元素分配至某些“桶”中,藉以达到排序的作用,基数排序法是属于稳定性的排序。其时间复杂度为 O (nlog(r)m),其中 r 为所采取的基数,而 m 为堆数,在某些时候,基数排序法的效率高于其它的稳定性排序法。'''import mathdef radix_sort(lists, radix=10):k = int(math.ceil(math.log(max(lists), radix)))bucket = [[] for i in range(radix)]for i in range(1, k+1):for j in lists:bucket[j/(radix**(i-1)) % (radix**i)].append(j)del lists[:]for z in bucket:lists += zdel z[:]return lists--------------------- 作者:CRazyDOgen 来源:CSDN 原文:https://blog.csdn.net/jipang6225/article/details/79975312 版权声明:本文为博主原创文章,转载请附上博文链接!

# 合成一个字典

ab = dict(zip(a, b))

# 根据字典的键进行排序(也就是第一个列表);也可以根据第二个列表进行排序。

# 具体是升序还是降序,自己挑着来。

ab_order = sorted(ab.items(), key=lambda x: x[0], reverse=

False)