Python入门的36个例子 之 19
源代码下载:下载地址在这里
# 022
listNum1 = [1, 3]
listNum2 = [2, 4]
listStr1 = ['a', 'c']
listStr2 = ['b', 'd']
# 列表的合并
list1 = listNum1 + listStr1
for ele in list1:
print ele
print '\n'
# 判断列表中是否包含某元素
print 'b' in list1
print 1 in list1
# 删除某个元素
for ele in list1:
print ele
del list1[1]
for ele in list1:
print ele
print '\n'
# 对列表进行排序
list1 = listNum1 + listNum2 + listStr1 + listStr2
for ele in list1:
print ele
print '\n'
list1.sort()
for ele in list1:
print ele
print '\n'
# 对列表进行倒排序
list1 = listNum1 + listNum2 + listStr1 + listStr2
for ele in list1:
print ele
print '\n'
list1.sort(reverse = True)
for ele in list1:
print ele
print '\n'
# 对列表进行逆序
list1 = listNum1 + listNum2 + listStr1 + listStr2
for ele in list1:
print ele
print '\n'
list1.reverse()
for ele in list1:
print ele
print '\n'
# 删除列表中连续的一部分
list2 = listNum1 + listNum2 + listStr1 + listStr2
list1 = list2
list1.sort()
for ele in list1:
print ele
print '\n'
del list1[0:2]
for ele in list1:
print ele
print '\n'
# Python中列表比较的是值而不是引用
list3 = [1, 3, 'a', 'c']
list4 = listNum1 + listStr1
print list3 == list4, '\n'
# 列表竟然可以比较大小
list5 = ['b', 'c']
list6 = ['a', 'z']
print list5 > list6, '\n'
# 提取元素
list1 = listNum1 + listNum2 + listStr1 + listStr2
print list1
print list1[7]
print list1[0:4]
# 列表的拼接
list1 = [1,2,3]
list2 = [2,3,4]
list1 += list2
print list1, '\n'
# 列表的翻倍
list1 = [1,2,3]
i = 3
list1 *= i
print list1
# 添加元素的两种方法
list1 = [1,2,3]
list1 += [4]
print list1
list1 = [1,2,3]
list1.append(4)
print list1
# 取得元素个数的方法
list1 = [1,2,3]
print len(list1)
# 取得特定元素的个数
list1 = [1,2,2,3,4,4,4]
print list1.count(2)
print list1.count(4), '\n'
# 在列表中寻找特定的元素
list1 = ['a', 'b', 'c', 'a', 'b', 'c', 'a']
print list1.index('a')
print list1.index('a', 1)
相关文档:
from: http://www.cnblogs.com/dahuzizyd/archive/2005/03/01/111006.html
python支持面向对象的编程风格,这里主要说说python中的多继承:
下面的代码使用python2.4,安装后使用idle的IDE开发环境(说是IDE ,比起delphi,VS.net等简单得太多了)
从File-New菜单建立一个.py文件,写下面的代码:
class SuperCl ......
import time,thread
def test(a,b):
for i in range(a,b):
time.sleep(1)
print i
def start():
thread.start_new_thread(test,(1,1001))
thread.start_new_thread(test,(1000,2001))
if __name__=='__main__':
start()
......
Python中的easy_install工具很好用,它的作用类似于Php中的pear,或者Ruby中的gem,或者Perl中的cpan。
如果想使用easy_install工具,可以直接安装ez_setup.py
脚本,再python ez_setup.py(之前先要安装python):
安装完后,最好确保easy_install所在目录已经被加到PATH环境变量里:
Windows: C:\Python25\Scripts
Li ......
# 015
# 默认参数的本质是:
# 不论你是否提供给我这个参数,我都是要用它,
# 那么我就要想好在你不向我提供参数的时候我该使用什么。
# 或者,这样来理解:
# 有一些参数在一般情况下是约定俗成的,
# 但,在极少情况下会有一些很有个性的人会打破传统而按照自己的习惯来做事
def theFirstDayInAWeek(theDay = 'Sunda ......
代码很简单,不到5k行。但是思路挺好的,改成non-blocking了之后效率就是能提高不少,特别是考虑到现代的web app都需要和其他的
HTTP服务器通信,blocking的代价太大了。 Tornado is an open source version of the scalable, non-blocking web server and tools that power FriendFeed. The FriendFeed application ......