python每日练习10题2

 163、一个字典 key 是人名、value 是年龄,找出其中年龄 最大的人

 

d = {"张三":25,"李四":30,"王五":80,"李三":80}
#result = sorted(d.items(),key = lambda x:x[1] ,reverse = True)
max_value = max(d.values())
keys_list=[]
for k,v in d.items():
if v == max_value:
keys_list.append(k)
print("年龄最大的人是:",keys_list)

 

164 、定义函数,用户输入 n 个字符串,输出排好序的字符 串
def sort_str(n):
result = []
for i in range(n):
s = input("请输入字符串:")
sorted_s = "".join(sorted(list(s)))
result.append(sorted_s)
return result
print(sort_str(3))
165 、一个列表的元素均是字符串,求其中长度最小的字符 串
l = ["huhogniang","name","sex","abc"]
sorted_l = sorted(l,key = len) #默认是升序
print(list(filter(lambda x:len(x) == len(sorted_l[0]),sorted_l)))
l = ["huhogniang","name","sex","abc"]
result = []
min_length = len(l[0])
for i in l:
if len(i)
min_length = len(i)
print(min_length)
for i in l:
if len(i) == min_length:
result.append(i)
print(result)
166 、找出一个字符串中,重复出现的字母和出现次数
代码
s = "aabbcccddgk"
d ={}
for i in s:
if i in d:
d[i] += 1
else:
d[i] =1
print(d)
167 、删除字符串中的重复字符(重复字符只保留一个) ?
代码
s = "aabbcccddgk"
d ={}
for i in s:
if i in d:
d[i] += 1
else:
d[i] =1
print(d)
result = []
for k,v in d.items():
result.append(k)
print("".join(result))
方法2:
s = "aabbcccddgk"
a =[]
for i in s:
if i not in a:
a.append(i)
print("".join(a))
168 、定义一个函数,形参定义为可变参数,返回所有传入 参数的数字和;用户键盘输入一个整数 n, 随机生成 n 个三位 数,利用定义的函数求随机生成的 n 个数字的和(提示:利 用解包)
def get_sum(*arg,**kw):
result = 0
for i in arg:
if isinstance(i,(int,float)):
result += i
for k,v in kw.items():
if isinstance(k,(int,float)):
result += k
if isinstance(v,(int,float)):
result += v
return result
input_num = int(input("请输入一个整数:"))
import random
num_list = []
for i in input_num:
num_list.append(random.randint(100,999))
#利用解包
print(get_sum(*num_list))

 

转载于:https://www.cnblogs.com/ff-gaofeng/p/10913155.html

你可能感兴趣的:(python每日练习10题2)