-
利用列表推导式, 完成以下需求:
a. 生成一个存放1-100中各位数为3的数据列表:
结果为 [3, 13, 23, 33, 43, 53, 63, 73, 83, 93] list1 = [i for i in range(3, 100, 10)] print(list1)
b. 利用列表推到是将 列表中的整数提取出来:
例如:[True, 17, "hello", "bye", 98, 34, 21] --- [17, 98, 34, 21] list1 = [i for i in [True, 17, "hello", "bye", 98, 34, 21] if type(i) == int] print(list1)
c. 利用列表推导式 存放指定列表中字符串的长度:
例如 ["good", "nice", "see you", "bye"] --- [4, 4, 7, 3] list1 = [len(i) for i in ["good", "nice", "see you", "bye"]] print(list1)
d. dict_list = [{“科目”:“政治”, “成绩”:98}, {“科目”:“语文”, “成绩”:77}, {“科目”:“数学”, “成绩”:99}, {“科目”:“历史”, “成绩”:65}]
去除列表中成绩小于70的字典 【列表推导式完成】
结果为: [{“科目”:“政治”, “成绩”:98}, {“科目”:“语文”, “成绩”:77}, {“科目”:“数学”, “成绩”:99}] dict_list = [{"科目": "政治", "成绩": 98}, {"科目": "语文", "成绩": 77}, {"科目": "数学", "成绩": 99}, {"科目": "历史", "成绩": 65}] new_dic = [key for key in dict_list if key["成绩"] >= 70] print(new_dic)
-
编写函数,求1+2+3+…N的和
def num_sun(num): total = 0 for i in range(num+1): total += i print(total) num_sun(100)
-
编写一个函数,求多个数中的最大值
def num_max(*num): print(max(num)) num_max(10, 23, 45, 99, 101)
-
编写一个函数,实现摇骰子的功能,打印N个骰子的点数和
import random def dice(N): total = 0 for i in range(N): point = random.randint(1, 6) total += point print(point) print(total) print(dice(3))
-
编写一个函数,交换指定字典的key和value。
例如:dict1={'a':1, 'b':2, 'c':3} --> dict1={1:'a', 2:'b', 3:'c'}
def exchange(dic1):
new_dic = {dic1[key]: key for key in dic1}
print(new_dic)
dic1={'a': 1, 'b': 2, 'c': 3}
print(exchange(dic1))
-
编写一个函数,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串
例如: 传入'12a&bc12d-+' --> 'abcd' def splice(str1): new_str = '' for i in str1: if 'a' <= i <= 'z' or 'A' <= i <= 'Z': new_str += i print(new_str) splice('12a&bc12d-+')
-
写一个函数,求多个数的平均值
def mean(*num): if not num: return None print(sum(num)/len(num)) mean(1, 5, 4, 7, 8, 10)
-
写一个函数,默认求10的阶乘,也可以求其他数字的阶乘
def factorial(num=10): total = 1 for i in range(1, num+1): total *= i print(total) factorial() factorial(5)
=======注意:以下方法不能使用系统提供的方法和函数,全部自己写逻辑
-
写一个自己的capitalize函数,能够将指定字符串的首字母变成大写字母
例如: 'abc' -> 'Abc' '12asd' --> '12asd' def capitalize_self(str): new_str = '' if str: first = str[0] if 'a' <= first <= 'z': cap = chr(ord(first)-32) new_str = cap + str[1:] print(new_str) else: print(str1) capitalize_self('12asd')
-
写一个自己的endswith函数,判断一个字符串是否已指定的字符串结束
例如: 字符串1:'abc231ab' 字符串2:'ab' 函数结果为: True 字符串1:'abc231ab' 字符串2:'ab1' 函数结果为: False def endswith_self(str1, str2): len1 = len(str1) len2 = len(str2) for i in range(max(len1, len2)): if str1[len1-len2:] == str2: print(True) break else: print(False) break endswith_self('abc231ab', 'ab') endswith_self('abc231ab', 'ab1')
-
写一个自己的isdigit函数,判断一个字符串是否是纯数字字符串
例如: '1234921' 结果: True
'23函数' 结果: False
'a2390' 结果: False
def isdigit_self(str):
count = 0
for i in str:
if '0' <= i <= '9':
count += 1
if count == len(str):
print(True)
else:
print(False)
isdigit_self('1234921')
isdigit_self('23函数')
isdigit_self('a2390')
-
写一个自己的upper函数,将一个字符串中所有的小写字母变成大写字母
例如: 'abH23好rp1' 结果: 'ABH23好RP1' def upper_self(str): new_str = '' for i in str: if 'a' <= i <= 'z': up = chr(ord(i)-32) new_str += up else: new_str += i print(new_str) upper_self('abH23好rp1')
-
写一个自己的rjust函数,创建一个字符串的长度是指定长度,原字符串在新字符串中右对齐,剩下的部分用指定的字符填充
例如: 原字符:'abc' 宽度: 7 字符:'^' 结果: '^^^^abc'
原字符:'你好吗' 宽度: 5 字符:'0' 结果: '00你好吗'
def rjust(str1, width, fill_char):
le = len(str1)
# 处理指定宽度比原字符串宽度小
if width < le:
width = le
# 处理新字符串长度不是1的情况
if len(fill_char) != 1:
raise ValueError
new_str = fill_char*(width-le) + str1
print(new_str)
rj_self('abc', 7, '^')
rj_self('你好吗', 5, '0')
-
写一个自己的index函数,统计指定列表中指定元素的所有下标,如果列表中没有指定元素返回-1
例如: 列表: [1, 2, 45, 'abc', 1, '你好', 1, 0] 元素: 1 结果: 0,4,6 列表: ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权'] 元素: '赵云' 结果: 0,4 列表: ['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权'] 元素: '关羽' 结果: -1 def index_self(list, char): flag = False for index in range(len(list)): if list[index] == char: print(index) flag = True if flag == False: print(-1) index_self([1, 2, 45, 'abc', 1, '你好', 1, 0], 1) index_self(['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权'], '赵云') index_self(['赵云', '郭嘉', '诸葛亮', '曹操', '赵云', '孙权'], '关羽')
-
写一个自己的len函数,统计指定序列中元素的个数
例如: 序列:[1, 3, 5, 6] 结果: 4 序列:(1, 34, 'a', 45, 'bbb') 结果: 5 序列:'hello w' 结果: 7 def len_self(char): total = 0 for i in char: total += 1 print(total) len_self([1, 3, 5, 6]) len_self((1, 34, 'a', 45, 'bbb')) len_self('hello w')
-
写一个自己的max函数,获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
例如: 序列:[-7, -12, -1, -9] 结果: -1 序列:'abcdpzasdz' 结果: 'z' 序列:{'小明':90, '张三': 76, '路飞':30, '小花': 98} 结果: 98 def max1(seq): if type(seq) == dict: seq = seq.values() if type(seq) not in (tuple, list): seq = list(seq) temp = seq[0] for i in seq[1:]: if temp < i: temp = i print(f'最大值:{temp}') max1([-7, -12, -1, -9]) max1('abcdpzasdz') max1({'小明':90, '张三': 76, '路飞':30, '小花': 98})
-
写一个函数实现自己in操作,判断指定序列中,指定的元素是否存在
例如: 序列: (12, 90, 'abc') 元素: '90' 结果: False 序列: [12, 90, 'abc'] 元素: 90 结果: True def in_self(list, char): flag = False for index in range(len(list)): if list[index] == char: flag = True if flag: print(True) else: print(False) in_self((12, 90, 'abc'), '90') in_self([12, 90, 'abc'], 90)
-
写一个自己的replace函数,将指定字符串中指定的旧字符串转换成指定的新字符串
例如: 原字符串: 'how are you? and you?' 旧字符串: 'you' 新字符串:'me' 结果: 'how are me? and me?' # 方法一: def replace_self(str, old_str, new_str): index = 0 len1 = len(str) len2 = len(old_str) new_str1 = '' while index <= len1 - 1: if old_str == str[index:index+len2]: new_str1 += new_str index += len2 else: new_str1 += str[index] index += 1 print(new_str1) replace_self('how are you? and you?', 'you', 'me') # 方法二: def replace(str1, old, new): s = str1.split(old) re = new.join(s) print(re) s1 = 'how are you? and you?' replace(s1, 'you', 'me')