基礎面試題
1. 選出字典中所有值爲真的元素
d = {i: i for i in range(10)}
d.update({str(i): None for i in range(1000000)})
# 以下是參考答案
r = {i[0]: i[-1] for i in d.items() if i[-1]}
print(r)
2.對列表中的學生信息列表按照’age’降序排序
lst = [{'name': '小明', 'age': 10}, {'name': '小紅', 'age': 11}]
# 以下是參考答案
r = sorted(lst, key=lambda x: x['age'], reverse=True)
print(r)
3.統計字符串中有多少種不同的字符, 並給出最多的前三個字符及其個數
from collections import Counter
s = "1234356897089234234345345789890"
c = Counter(s)
print(len(c))
print(c.most_common(3))