defget_alpha(string:str):# 方法一:# new_str = ''# for x in string:# if 'a' <= x <= 'z' or 'A' <= x <= 'Z':# new_str += x# print(new_str)# 方法二:# s1 = [x for x in string if 'a' <= x <= 'z' or 'A' <= x <= 'Z']# new_str = ''.join(s1)# print(new_str)
new_str1 =''.join([x for x in string if'a'<= x <='z'or'A'<= x <='Z'])print(new_str1)
str1 ='你好, Python!123, h5'
get_alpha(str1)
deflen1(seq):
count =0for _ in seq:
count +=1print(count)
10.写一个自己的max函数,获取指定序列中元素的最大值。如果序列是字典,取字典值的最大值
ddef in_function(seq):iftype(seq)==dict:
result =[i for i in seq.values()]
num = result[0]for j in result:if num < j:
num = j
print(num)else:
m = seq[0]for i in seq[1:]:if i > m:
m = i
print(m)
in_function({'小明':90,'张三':76,'路飞':30,'小花':98})
11.写一个函数实现自己in操作,判断指定序列中,指定的元素是否存在
defin1(seq, item):for x in seq:if x == item:print(True)breakelse:print(False)
12.写一个自己的replace函数,将指定字符串中指定的旧字符串转换成指定的新字符串
defreplace(string:str, old:str, new:str):# 方法一:# new_str = new.join(string.split(old))# print(new_str)# 方法二:
length1 =len(string)
length2 =len(old)
index =0
new_str =''whileTrue:if string[index: index+length2]== old:
new_str += new
index += length2
else:
new_str += string[index]
index +=1if index >= length1:breakprint(new_str)
replace('how are you?',' ','+')
replace('how are you?','you','me')