python基础面试题
一、给定一个字符串,将字符串的偶数位的字母替换成*
l = ‘hu21w3ad234eww3123478965hjawk1212’
li = []
for i in l:
li.append(i)
for n in range(len(li)):
if (n+1) % 2 == 0 and li[n].isdigit():
li[n] = li[n].replace(li[n], "*")
else:
continue
print("".join(li))
二、输入三个整数x,y,z,请把这三个数由小到大输出。
li = []
for i in range(3):
a = int(input(“请输入整数”))
li.append(a)
li.sort()
print(",".join(li))
三、计算字符串中字符出现的次数
l = ‘hu21w3ad234eww3123478965hjawk1212’
d = {}
for i in l:
d[i] = l.count(i)
print(d)
dn = 0
for n in sorted(d):
print(
“%s 出现的次数是%s” % (n, d[n])
)
四、list中放一个字典,name和age,不改变list的情况下,设计算法让name按照age升序排列。
li = [
{“name”: “xiaoming”, “age”: 22},
{“name”: “xiaogang”, “age”: 13},
{“name”: “limin”, “age”: 19},
{“name”: “wkqlj”, “age”: 42},
{“name”: “AA”, “age”: 2}
]
import operator
li.sort(key=operator.itemgetter(‘age’), reverse=False)
print(li)