python 测试常见面试题

1、统计元素的个数:
a = [1, 2, 3, 1, 1, 2]
dict = {}
for key in a:
dict[key] = dict.get(key,0) + 1
print(dict)

def count(m):
for i in a:
result = a.count(m)
print(result)

if __name__ == "__main__":
count(1)
2、冒泡排序
def  bubble_sort(num):
for i in range(len(num)-1): #这个循环负责设置冒泡排序进行的次数,即循环的趟数
for j in range(len(num)-1-i): #i的初始值为0,每次循环确定一个值,循环一次,减少一趟
if num[j] > num[j+1]:
num[j],num[j+1] = num[j+1],num[j]
return num
if __name__ == "__main__":
num = [1,2,3,4,9,5,2,3,8]
print(bubble_sort(num))

3、处理一维数
li = []
arr = [["zhang"],["wamg"],["li"],["zhao"]]
for i in arr:
for j in i:
li.append(j)
print(li)

4.两个列表转成字典
list1 = [1,2,3]
list2 = ["a","b","c",]

dict = dict(zip(list1,list2))
print(dict)

5.洗牌游戏
import random

def shuffle(lis):
result = []
while lis:
p = random.randrange(len(lis))
result.append(lis[p])
lis.pop(p)
return result

r = shuffle([1,2,3,4,6,7,8,9,10])
print(r)


  import random
  alist = [1,2,3,4,5]
  random.shuffle(alist)
  print(alist)




6.计算列表中正数和负数的个数
j = []
j1 = []
f = [1, 2, 3, 4, -1,-6]

for i in f:
if i <= 0:
j.append(i)
if i >= 0:
j1.append(i)
print(len(j))
print(len(j1))
7. 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?
解法一: f = [(x,y,z) for x in range(1,5) for y in range(1,5) for z in range(1,5) if (x !=y) if(x !=z) if (y != z)]
for i in f:
print(i,f.count(i))
解法二:
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if( i != k ) and (i != j) and (j != k):
print (i,j,k)
8.输入日期,判断这一天是这一年中的第几天import  datetime

def d():
year = input("请输入年份:")
month = input("请输入月份:")
day = input("请输入天")
day1 = datetime.date(year=int(year),month=int(month),day=int(day))
print(day1)

day2 = datetime.date(year=int(year),month=1,day=1)
print(day2)
return (day1-day2).days+1


if __name__ == "__main__":
result = d()
print(result)

9. 对字典 f= {"name":"5","age":"2","birthday":"6"} 按照value 进行排序
  f= {"name":"5","age":"2","birthday":"6"}
  s = []
  for k,v in f.items():
s.append(v)
s.sort()
  print(s)

10.#对列表进行反转 f1 = [3,2,4,5,7] print(f1[::-1]) #对字符串进行反转 print("abcde"[::-1])
11.
list 对象 alist [{'name':'a','age':20},{'name':'b','age':30},{'name':'c','age':25}],按 alist 中元素的age 由大到小排序
alist = [{'name':'a','age':20},{'name':'b','age':30},{'name':'c','age':25}]
def sort_by_age(alist):
return sorted(alist,key=lambda x:x["age"],reverse=True)
if __name__ == "__main__":
result = sort_by_age(alist)
print(result)

12.写一个列表生成式,产生一个公差为11的等差数列
f = [ x*11 for x in range(1,11)] print(f)

13.
列表去重
f = [1,2,3,4,1,2] f1 = set(f) print(f1)

14.
找出列表中相同和不同元素
f = [1,2,3,4,5]
f1 = [1,2,3]
#找出相同元素
print(set(f) & set(f1))
#找出不同
print(set(f) ^ set(f1))

15.
一行代码实现1-100之和
print(sum(range(1,101)))

16.统计一个文本单词频次最高的两个单词
import  re
def test(filepath):
distone = {}
with open(filepath) as f:
for line in f:
#匹配所有字母,空格代替
line = re.sub("\W+", " ", line)
lineone = line.split()
for keyone in lineone:
if not distone.get(keyone):
distone[keyone] = 1
else:
distone[keyone] += 1
num_ten = sorted(distone.items(), key=lambda x: x[1], reverse=True)[:2]
return num_ten


if __name__ == "__main__":
s = test(r"C:\demo\file.txt")
print(s)
15. 实现reverser 函数
def  rev(s):
start = 0
end = len(s) - 1
while start < end:
s[start],s[end] = s[end],s[start]
start +=1
end -= 1
return s
if __name__ == "__main__":
print(rev([1,2,3,4]))
16. 按照文件后缀整理文件夹
import  os
import shutil

dir =r"C:\example\test"
def test_ext(path):
for file in os.listdir(path):
ext = os.path.splitext(file)[1]
#获取文件后缀
ext = ext[1:]
#判断不同后缀的文件夹是否存在,不存在则创建
if not os.path.isdir(f"{dir}/{ext}"):
os.mkdir(f"{dir}/{ext}")
#按照不同文件后缀进行归纳到不同文件夹下
soure_path = f"{dir}/{file}"
target_path = f"{dir}/{ext}/{file}"
shutil.move(soure_path,target_path)

if __name__ == "__main__":
test_ext(dir)





 
 
 
posted @ 2021-08-12 20:27  微风轻哨  阅读(791)  评论(0编辑  收藏  举报