判断 数字 字母 空格 总和 分别多少。 def funk_1(p): number = 0 letter = 0 space = 0 other = 0 for i in p: other += 1 if i.isdigit(): number += 1 elif i.isalpha(): letter += 1 elif i.isspace(): space += 1 print(number,letter,space,other) funk_1(a)
判断(字符串、列表、元组)长度是否大于5。 b = 'tian' b1 = ['eekj;d'] b2 = ('sdfasdfa') def funk_2(a,a1,a2): str_1 = True if len(a)>5 else False list_1 = True if len(a1)>5 else False tuple_1 = True if len(a2)>5 else False print(str_1,list_1,tuple_1) funk_2(b,b1,b2)
进化
b = 'tiavvn' def funk_2(a): if isinstance(a,str): return True if len(a)>5 else False t = funk_2(b) print(t)
检查传入列表的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者 ee = [] e = ['a1l4i','ret','toy'] def funk_5(a): for i in a: a = i[0:2] ee.append(a) return(ee) funk_5(e)
检查获取传入列表或元组对象的所有奇数位索引对应的元素,并将其作为新列表返回给调用者。 yy = [] y = [11,22,33,44,55,66,77] def funk_6(a): for i in a: if i%2 == 1: u = y.index(i) print(u) yy.append(u) print(yy) funk_6(y)
检查传入字典的每一个value的长度,如果大于2,那么仅保留前两个长度的内容,并将新内容返回给调用者 b1 = [] b = {'k1':'asf','k2':'fda'} def funk_7(a): for i in a: y = b[i] u = y[0:2] b1.append(u) print(b1) funk_7(b)
检查用户传入的对象(字符串、列表、元组)的每一个元素是否含有空内容 c = 's a' c1 = ['s df'] c2 = ('da f','s f af',' saf') def funk_3(a,a1,a2): str_1 = 0 list_1 = 0 tuple_1 = 0 for i in a: if i.isspace(): str_1 += 1 print('str有%s个'%str_1) for i1 in a1: for i11 in i1: if i11.isspace(): list_1 += 1 print('list 有%s个'%list_1) for i2 in a2: for i22 in i2: if i22.isspace(): tuple_1 += 1 print('tuple 有%s个'%tuple_1) funk_3(c,c1,c2)
进化 c = 's a' def funk_3(a): ret = True for i in a: if i.isspace(): ret = False break return ret y = funk_3(c) print(y)