Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Learning #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Errors
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#1、a=[1,2,3,4,5]。则 a[5:0]==[] , 而不会报错
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
11 changes: 10 additions & 1 deletion differences
Original file line number Diff line number Diff line change
@@ -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 <class'int'>.In python 2.7,it returns <type'int'>.(Which may explain sth between class and type?)
27 changes: 27 additions & 0 deletions test1.py
Original file line number Diff line number Diff line change
@@ -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 "测试失败"

58 changes: 58 additions & 0 deletions test2.py
Original file line number Diff line number Diff line change
@@ -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('测试失败')
33 changes: 33 additions & 0 deletions test4.py
Original file line number Diff line number Diff line change
@@ -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 "测试失败"