Skip to content

Commit 46e1c84

Browse files
author
kangxiaoyu
committed
切片,迭代,列表生成式的练习
1 parent 3aa2933 commit 46e1c84

22 files changed

+144
-0
lines changed

2017-04-21/decorate.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/env python
2+
3+
4+
# -*- coding:utf-8 -*-
5+
6+
def log(func):
7+
def wrapper(*args,**kw):
8+
print('call %s():' % func.__name__)
9+
return func(*args,**kw)
10+
return wrapper
11+
@log
12+
def now():
13+
print('2017-04-21')
14+
now()
15+

2017-04-24/high_grade_feature/odd.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
L=[]
4+
n=1
5+
while n <=99:
6+
L.append(n)
7+
n=n+2
8+
print L

2017-04-24/itera/for_2_variable.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
from collections import Iterable
4+
for x,y in [(1,1),(2,4),(3,9)]:
5+
print(x,y)

2017-04-24/itera/iterable2.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
list=['kping','sguilian','xiaofei','xiaoxiao']
4+
for(i=0;i<list.length;i++){
5+
n = list[i];
6+
print n;
7+
}

2017-04-24/itera/iterable3.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/env python
2+
d={'a':1,'b':2,'c':3}
3+
for key in d:
4+
print(key)

2017-04-24/itera/iteratale_str.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
#这里可以写中文
4+
for ch in 'abc':
5+
print(ch)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
from collections import Iterable
4+
for i,value in enumerate(['A','B','C']):
5+
print(i,value)
6+

2017-04-24/itera/use_iterable.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
from collections import Iterable
4+
print isinstance('abc',Iterable)
5+
print isinstance([1,2,3],Iterable)
6+
print isinstance(123,Iterable)
7+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
L=['Hello','world','ibm','apple']
4+
print [s.lower() for s in L]

2017-04-24/list_generate/list_g1.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import os
2+
print [d for d in os.listdir('.')]

2017-04-24/list_generate/list_g2.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import os
2+
d={'x':'A','y':'B','z':'C'}
3+
for k,v in d.items():
4+
print(k, '=', v)

2017-04-24/list_generate/list_g3.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import os
2+
d={'x':'A','y':'B','z':'C'}
3+
print [k + '=' + v for k, v in d.items()]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
# 测试中文
4+
print list(range(1,11))
5+
L=[]
6+
for x in range(1,11):
7+
L.append(x*x)
8+
print L
9+
print [x*x for x in range(1,11)]
10+
print [x*x for x in range(1,11) if x%2 == 0]
11+
12+
print [m + n for m in 'ABC' for n in 'XYZ']
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
L = ['Hello', 'World', 18, 'Apple', None]
4+
print [s.lower() for s in L if isinstance(s, str) ]

2017-04-24/slice/last_slice.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
L=['Michael','Sarah','Tracy','Bob','Jack']
4+
print L[-1]
5+
#print [-2:]
6+
print L[-2:-1]

2017-04-24/slice/no_slice1.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
# 这里可以写中文呀.
4+
L=['Michael','Sarah','Tracy','Bob','Jack']
5+
6+
print L[0],L[1],L[2]

2017-04-24/slice/no_slice2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
# 这里可以写中文呀.
4+
L=['Michael','Sarah','Tracy','Bob','Jack']
5+
r=[]
6+
n=3
7+
for i in range(n):
8+
r.append(L[i])
9+
print r
10+
#print L[0],L[1],L[2]

2017-04-24/slice/slice.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
L=['Michael','Sarah','Tracy','Bob','Jack']
4+
print L[0:3]

2017-04-24/slice/slice11.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
L=['Michael','Sarah','Tracy','Bob','Jack']
4+
print L[:3]

2017-04-24/slice/slice22.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
L=['Michael','Sarah','Tracy','Bob','Jack']
4+
print L[1:3]

2017-04-24/slice/userful_slice.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
L=list(range(100))
4+
print L
5+
print L[:10]
6+
print L[-10:]
7+
print L[10:20]
8+
print L[:10:2]
9+
print L[::5]
10+
#甚至什么都不写, 只写[:]就可以原样复制一个list
11+
print L[:]
12+
#tuple也是一种list,唯一区别式tuple不可变,因此,tuple也可以用切片操作,只是操作的结果仍是tuple
13+
print (0,1,2,3,4,5)[:3]
14+

2017-04-24/slice/userful_slice2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
#字符串'xxx'也可以看成是一种list,每个元素就是一个字符。因此,字符串也可以用切片操作,只是操作结果仍是字符串:
4+
print 'ABCDEFG'[:3]
5+
print 'ABCDEFG'[::2]
6+
#在很多编程语言中,针对字符串提供了很多各种截取函数(例如,substring),其实目的就是对字符串切片。Python没有针对字符串的截取函数,只需要切片一个操作就可以完成,非常简单。
7+
8+
#小结
9+
10+
#有了切片操作,很多地方循环就不再需要了。Python的切片非常灵活,一行代码就可以实现很多行循环才能完成的操作。

0 commit comments

Comments
 (0)