Python
fashion_man
这个作者很懒,什么都没留下…
展开
-
Python:利用filter()筛选出回数
# -*- coding: utf-8 -*-def is_palindrome(n): # 将字符串反转后和原字符串进行比较,相同时返回 return str(n) == str(n)[::-1]# 测试代码output = filter(is_palindrome, range(1, 1000))print('1~1000:', list(output))if ...原创 2018-09-03 08:47:50 · 2950 阅读 · 1 评论 -
Python:利用map()函数,把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。
# -*- coding: utf-8 -*-def normalize(name): # 使用capitalize()函数将字符串的首字母转为大写,其余变为小写 return name.capitalize()# 测试结果是否正确L1 = ['adam', 'LISA', 'barT']L2 = list(map(normalize, L1))print(L2)...原创 2018-09-02 20:47:06 · 939 阅读 · 0 评论 -
Python:利用map和reduce编写一个str2float函数,把字符串'123.456'转换成浮点数123.456
# -*- coding: utf-8 -*-from functools import reducedef str2float(s): def fn(x, y): return x * 10 + y def char2num(s): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5...原创 2018-09-02 20:44:07 · 8555 阅读 · 8 评论 -
Python:if语句练习
题目1:通过input函数,任意输入三条边长,经过简单的计算后,判断三条边长是否构成三角形并确定是什么样类型的三角形(等腰三角形,等边三角形,一般三角形等)# -*- coding: utf-8 -*-def triangle(): a = int(input("请输入三角形边长:")) b = int(input("请输入三角形边长:")) c = int(inp...原创 2018-10-20 11:48:34 · 2901 阅读 · 0 评论