1、统计字母、数字、空格以及其它的个数
1 def check(list): 2 alpha = 0 3 num = 0 4 space = 0 5 ether = 0 6 for i in list: 7 if i.isalpha(): 8 alpha += 1 9 elif i.isdigit(): 10 num += 1 11 elif i.isspace(): 12 space += 1 13 else: 14 ether += 1 15 s = "alpha有{}个 num{}个 spe{}个 e有{}个".format(alpha, num, space, ether) 16 return s
2、判断用户传入的对象(字符串、列表、元组)的长度是否大于5
1 str1 = "dfsdfdsfsd" 2 str2 = [1,2,3,4,5,6] 3 str3 = (1,2,3) 4 def check_len(str): 5 if len(str) > 5: 6 return True 7 else: 8 return False
3、检查传入列表的长度,大于2,保留前2个长度的内容,并将新内容返回
1 def check_space(s): 2 if len(s): 3 return s[:2] 4 print(check_space(str3))
4、检查传入字典的每一个value的长度,如果大于2,保留前两个的长度的内容,并将新内容返回
1 dic = {"k1":"v1v1","k2":[11,22,33,44]} 2 def item(conn): 3 for key,value in conn.items(): 4 if len(value) > 2: 5 dic[key]= value[:2] 6 return conn