diff --git a/Errors b/Errors new file mode 100644 index 0000000..197b1c5 --- /dev/null +++ b/Errors @@ -0,0 +1 @@ +#1、a=[1,2,3,4,5]。则 a[5:0]==[] , 而不会报错 diff --git a/README.md b/README.md index f3b0583..e521da8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # Learning -Some exersices of learing and notes. +# Some exersices of learing and notes. + +# Don't now what should I say.Just do something change to test the function. diff --git a/differences b/differences index 1a518ab..9280a02 100644 --- a/differences +++ b/differences @@ -1,4 +1,13 @@ # Here are some differences between Python2.7 and Python 3,which may lead to bug. + #1、In Python 2.7,You can use "print \'str'".But in Python 3,after "print" you must use() to complete this statement. + #2、In Python 2.7,generator.next().In Python 3,next(generator). -#3、In python 2.7,what function "filter" returns is a list.But in python 3,what function "filter"returns is a generator. + +#3、In python 2.7,what function "filter" returns is a list.But in python 3,what function "filter"returns is a generator.(test2.py) + +#4、In python 2.7,None.split() will cause error.But in python 3,None.split() == ''(test2.py) + +#5、In python 3,There is a function "nonlocal".But in python 2.7,it doesn't exist.(test4.py) + +#6、In python 3,type(3)returns .In python 2.7,it returns .(Which may explain sth between class and type?) diff --git a/test1.py b/test1.py new file mode 100644 index 0000000..841b07e --- /dev/null +++ b/test1.py @@ -0,0 +1,27 @@ +#! /usr/bin/env python +# -*- coding=utf-8 -*- + +""" +练习map和reduce函数 +""" +from functools import reduce + +def str2float(s): + a1,a2 = s.split('.') #分隔符split() + def string(s): + dight = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}#字典用{} + return dight[s] + def num1(x,y): + return x*10+y + def num2(x,y): + return x/10.0+y #此处高能注意!x为整数,若x/10,则结果为0,所以此处采用/10.0,计算结果为小数 + zheng = reduce(num1,map(string,a1)) + xiao = reduce(num2,map(string,reversed(a2))) #字符串反转reversed(),注意reversed反转结果为iterator,要用list显示出 + return num2(xiao,zheng) + +print('str2float(\'123,456\') =',str2float('123.456')) +if abs (str2float('123.456') - 123.456) < 0.000001: + print "测试成功!" +else: + print "测试失败" + diff --git a/test2.py b/test2.py new file mode 100644 index 0000000..8f1a7f8 --- /dev/null +++ b/test2.py @@ -0,0 +1,58 @@ +#/usr/bin/env python +# -*- coding:utf-8 -*- + +""" + 测试filter函数 +""" +# 在3.0中 filter结果为iterator;2.7中则为list。所以如求素数的代码,循环过后就出问题,因为list没有next +""" +def not_empty(s): + return s and s.split() #""=false,所以此处判定结果ok + +print list(filter(not_empty,['A','','B','C',' '])) #此处出了点小问题,原题有None,None.split()会报错,python3.0则不会 +#引申,filter(None,a)会保留a中所有为真的元素,''、0等则会被剔除 +""" + +""" + +def _odd_iter(): + n = 1 + while True: + n = n + 2 + yield n + +def _not_divisible(n): + return lambda x:x % n >0 + +def primes(): + yield 2 + it = _odd_iter() + while True: + n = it.next() + yield n + it = filter(_not_divisible(n),it) + + +for n in primes(): + if n < 1000: + print(n) + else: + break +""" +def is_palindrome(n): + string=str(n) + length = len(string) + if length == 1: + return True + else: + for i in range(0,length//2+1): + if string[i] != string[length-i-1]: #小心,string索引最大为length-1,不是length + return False + return True + +output = filter(is_palindrome,range(1,1000)) +print('1~1000:', list(output)) +if list(filter(is_palindrome, range(1, 200))) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191]: + print('测试成功!') +else: + print('测试失败') diff --git a/test4.py b/test4.py new file mode 100644 index 0000000..ab315db --- /dev/null +++ b/test4.py @@ -0,0 +1,33 @@ +#! /usr/bin/env python +# -*- coding=utf-8 -*- + +""" + 测试sorted函数 + +L=[('Bob',75),('Adam',92),('Bart',66),('Lisa',88)] +def by_name(t): + return t[0].lower() + +def by_score(t): + return -t[1] + +print sorted(L,key=by_name) +print sorted(L,key=by_score) +""" +''' + 闭包 +''' +def CreateCounter(): + i = [0] + def counter(): + i[0] =i[0] + 1 #这一行不能放到return i[0]后面,用i[0]是因为int(i)无法调用,(外部变量),在python3.0中有函数nonlocal解决调用外部变量的问题 + return i[0] + return counter + +counterA = CreateCounter() +print(counterA(),counterA(),counterA(),counterA()) +counterB=CreateCounter() +if [counterB(),counterB(),counterB(),counterB()] == [1,2,3,4]: + print '测试通过!' +else: + print "测试失败"