您的位置:首页 > 编程语言 > Python开发

python从入门到精通,心含谦逊,好好学习

2018-03-14 17:08 113 查看
把L1中的转换成小写字母:L1 = ['Hello', 'World', 18, 'Apple', None]
L=[]
for i in range(len(L1)):
    if isinstance(L1[i],str)==True:
         L.append(L1[i])
L2=[s.lower() for s in L]---------------------------

当然也可以简单的写为:
L1 = ['Hello', 'World', 18, 'Apple', None]
L2=[s.lower()for s in L1 if isinstance(s,str)==True]      #注意是大写True,函数为isintance
print(L2)
if L2 == ['hello', 'world', 'apple']:
    print('测试通过!')
else:
    print('测试失败!')   --------------------------

一句话实现杨辉三角(大神作品)def triangles():
    l=[1]
    while 1:
        yield l
        l=[1]+[l
+l[n+1] for n in range(len(l)-1)]+[1]
# 期待输出:
# [1]
# [1, 1]
# [1, 2, 1]
# [1, 3, 3, 1]
# [1, 4, 6, 4, 1]
# [1, 5, 10, 10, 5, 1]
# [1, 6, 15, 20, 15, 6, 1]
# [1, 7, 21, 35, 35, 21, 7, 1]
# [1, 8, 28, 56, 70, 56, 28, 8, 1]
# [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
n = 0
results = []
for t in triangles():
    print(t)
    results.append(t)
    n = n + 1
    if n == 10:
        break
if results == [
    [1],
    [1, 1],
    [1, 2, 1],
    [1, 3, 3, 1],
    [1, 4, 6, 4, 1],
    [1, 5, 10, 10, 5, 1],
    [1, 6, 15, 20, 15, 6, 1],
    [1, 7, 21, 35, 35, 21, 7, 1],
    [1, 8, 28, 56, 70, 56, 28, 8, 1],
    [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
]:
    print('测试通过!')
else:
    print('测试失败!')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: