易截截图软件、单文件、免安装、纯绿色、仅160KB

python中的列表排序操作

 
 
 
Python lists have a built-in sort() method that modifies the list in-place and a sorted() built-in function that builds a new sorted list from an iterable.
There are many ways to use them to sort data and there doesn't appear to be a single, central place in the various manuals describing them, so I'll do so here.
 
Backward compatibility note
Many constructs given in the HOWTO assume Python 2.4. Before that, there was no sorted() builtin and list.sort() took no keyword arguments.
 
Sorting basic data types
A simple ascending sort is very easy: just call the sorted() function. It returns a new sorted list:
 
>>> print sorted([5, 2, 3, 1, 4])
[1, 2, 3, 4, 5]
You can also use the sort() method of a list. It modifies the list in-place (and returns None to avoid confusion). Usually it's less convenient than sorted() - but if you don't need the original list, it's slightly more efficient.
 
>>> a = [5, 2, 3, 1, 4]
>>> a.sort()
>>> print a
[1, 2, 3, 4, 5]
Sort takes an optional function which can be called for doing the comparisons. The default sort routine is equivalent to using cmp:
 
>>> print sorted([5, 2, 3, 1, 4], cmp)
[1, 2, 3, 4, 5]
where cmp() is the built-in function that compares two objects, x and y, and returns a negative number, 0 or a positive number depending on whether x<y, x==y, or x>y. During the course of the sort the relationships must stay the same for the final list to make sense.
If you want, you can define your own function for the comparison. For integers we can do:
 
>>> def numeric_compare(x, y):
>>> return x-y
>>>
Note that this does not work for numbers in general, as the comparison function must return integers.
For numbers in general, but a little more understandably:
 
>>> def numeric_compare(x, y):
>>> if x>y:
>>> return


相关文档:

(转)Unicode和Python的中文处理

      ——由于最近在做有关网页搜索的项目,涉及到一些编码方面的知识,小弟在网上偶然地发现了这么一篇文章,很易懂,不晦涩,为了方便自己也同时能方便大家,就转了过来,以作参考……
      文章出处:http://blog.csdn.net/tingsking18/arc ......

几道Python的小习题

学习Python的道路漫漫,光看不练比较无聊。
找了个网页,上面有几道习题,无聊之余拿来练手,还有些乐趣。
是这里:http://www.cnblogs.com/belaliu/archive/2006/11/25/572140.html
注:习题后面贴的代码不一定是最优的。
大部分比较好解决,有点难度的是第4题做去除字符串内的空格的操作。
找了网上的解决方案,有这 ......

Python Socket Server

今天做ftp的界面,做的相当郁闷,弄得心情及其不爽,在网上搜到死都不知道该怎么办,打算明天先看看C++
的是怎么弄的再说。不过,现在我想写一下关于socket的编程。
先写一个时间服务器吧,他监听端口,并且会返回 服务器的时间
server.py
#!/usr/bin/python
# Copyright (c) angelipin (angelipin@126.com)
import ......

(转) python应用领域介绍

#---------------------转转转转转转转转转转转转转转转转转转转转转转转-------------------------------------------#
Python作为一种功能强大且通用的编程语言而广受好评,它具有非常清晰的语法特点,适用于多种操作系统,目前在国际上非常流行,正在得到越来越多的应用。
 
  下面就让我们一起来看看它的强大 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号