python的典型实例

date: 2017-07-07 18:21:52

tags: python

nonlocal关键字用来在函数或其他作用域中使用外层(非全局)变量。

  1. 不知道字符串转大写是upper(),转小写是lower()

  2. 忘记字符串也可以切片了

  3. 字符串的连接原来可以用加号+

1
2
3
4
5
6
def normalize(name):
return name[0].upper()+name[1:].lower()
L1 = ['adam', 'LISA', 'barT']
L2 = list(map(normalize, L1))
print(L2)

name[0].upper() 是首字母大写
name[1:].lower() 从第二个字母开始全都小写

字符串转数的算法已经在示例中已经有了,也很好理解。转成浮点数的难点在于小数点两遍的数字处理方法不一样。我想肯定需要一个判断句,判断在小数点的左右,而采用不同的方法。同时,还要一个参数来记住小数点的位置。最终,又是参考各位大神,又是参照作者的答案。大概是懂了。

作者的答案:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from functools import reduce
CHAR_TO_FLOAT = {
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'.': -1
}
def str2float(s):
nums = map(lambda ch: CHAR_TO_FLOAT[ch], s)
point = 0
def to_float(f, n):
nonlocal point
if n == -1:
point = 1
return f
if point == 0:
return f * 10 + n
else:
point = point * 10
return f + n / point
return reduce(to_float, nums, 0.0)
print(str2float('0'))
print(str2float('123.456'))
print(str2float('123.45600'))
print(str2float('0.1234'))

由于to_float传入的数据都是数而不是字符,因此CHAR_TO_FLOAT里面定义.为0-9以外的数,加以区分。if n == -1:用来判断是否已经到小数点的位置,如果到了则改变point的值,point可以说是记录了当前小数点的位置,默认为0。然后根据point值来判断小数点的位置,以确定使用哪个转换算法。return reduce(to_float,nums,0.0)中有一项是0.0,是为了转换整数字符为浮点数。如果不添加0.0,则CHAR_TO_FLOAT里的value都要改成浮点数的形式,key值不用改,小数点对应的数值也可
以不改。

筛选出字符串类型并展示:
1
2
3
4
5
6
7
8
9
10
11
12
L1=['hello','world',18,'apple',None]
L2=[]
n=1
for s in L1:
if isinstance(s,str)==1:
L2.append(s.lower())
else:
print('the number of non str is %d ' %(n))
n=n+1
print(L2)
L2=[s.lower() for s in L1 if isinstance(s,str)==1]
print(L2)
斐波拉契数列
1
2
3
4
5
6
7
8
def fib(max):
n, a, b = 0, 0, 1
while n < max:
print(b)
a, b = b, a + b
n = n + 1
return 'done'
上面的函数可以输出斐波那契数列的前 N 个数:>>> fib(6)
杨辉三角
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def triangles():
j = 0
L1 = []
L2 = []
S1 = 0
while j < 10:
s = 0
for i in L1:
S1 = s + i
s = i #記錄上個循環i的值
L2.append(S1)
L2.append(1)
yield L2 #生成器 返回L2 list 列表的值
L1 = L2[:] #將L1指向L2 變成上一次循環的list
L2 = [] #L2保存的是當次循環的list 初始化她
j = j + 1
n = 0
for x in triangles():
print(x)
n = n + 1
if n == 10:
break
滤掉非回数

回数是指从左向右读和从右向左读都是一样的数,例如 12321 , 909 。请
利用 filter() 滤掉非回数

1
2
3
4
def i(n):
return str(n) == str(n)[::-1]
output = filter(i, range(1, 10000))
print(list(output))

把一个序列中的空字符串删掉
1
2
3
4
def not_empty(s):
return s and s.strip()
list(filter(not_empty, ['A', '', 'B', None, 'C', ' ']))
# 结果: ['A', 'B', 'C']
删掉偶数,只保留奇数
1
2
3
4
def is_odd(n):
return n % 2 == 1
list(filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15]))
# 结果: [1, 5, 9, 15]
将一个16进制的数转换为十进制:
1
2
3
def int2(x, base=16):
return int(x, base)
print(int2('12345'))
排序

假设我们用一组 tuple 表示学生名字和成绩:
L = [(‘Bob’, 75), (‘Adam’, 92), (‘Bart’, 66), (‘Lisa’, 88)]

请用 sorted() 对上述列表分别按成绩从高到低排序:

1
2
3
4
5
L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
def by_name(t):
return t[1]
L2 = sorted(L, key=by_name, reverse = True)
print(L2)

文章目录
  1. 1. tags: python
    1. 1.0.0.1. 筛选出字符串类型并展示:
    2. 1.0.0.2. 斐波拉契数列
    3. 1.0.0.3. 杨辉三角
    4. 1.0.0.4. 滤掉非回数
    5. 1.0.0.5. 把一个序列中的空字符串删掉
    6. 1.0.0.6. 删掉偶数,只保留奇数
    7. 1.0.0.7. 将一个16进制的数转换为十进制:
    8. 1.0.0.8. 排序