python编程题41-54
41.如何用python删除一个空文件,删除一个空目录?
import os # 引入文件操作库
def CEF(path):
"""
CLean empty files, 清理空文件夹和空文件
:param path: 文件路径,检查此文件路径下的子文件
:return: None
"""
files = os.listdir(path)# 获取路径下的子文件(夹)列表
for file in files:
print('Traversal at', file)
if os.path.isdir(file): # 如果是文件夹
if not os.listdir(file): # 如果子文件为空
os.rmdir(file) # 删除这个空文件夹
elif os.psth.isfile(file): # 如果是文件
if os.path.getsize(file) == 0: #文件大小为0
os.remove(file)# 删除这个文件
print(path,'Dispose over!')
if __name__ == "__main__": # 执行本文件则执行下述代码
CEF(path)
42.如何用python删除一个文件,删除一个非空目录
(1)删除一个文件
import os
my_file = '文件路径'
if os.path.exists(my_file): # 如果文件存在
#删除文件,可使用以下两种方法。
os.remove(my_file) # 则删除
#os.unlink(my_file)
else:
print('no such file:%s'%my_file)
(2) 删除一个非空目录
import shutil
path = '所删除文件夹路径'
shutil.rmtree(path)
43.如果让你用python设计一些有意思的小案例,你想做一些什么样的案例,列举案例中涉及到的主要知识点,至少需要设计三个案例。
案例一:根据用户输入的值判断相应的范围打印不同的话(多项分支)
h = input('please input height:')
height = float(h)
if 1<= height <=1.5:
print("小强 你在哪里?")
elif 1.5<height<=1.7:
print("没有安全感~")
elif 1.7<height<=1.8:
print("帅哥 留个电话")
elif 1.8<height<=2:
print("帅哥 你建议多一个女朋友吗")
please input height:1.6
没有安全感~
案例二:百钱百鸡: 公鸡 = 1 母鸡 = 3 小鸡 = 0.5 100块钱 买 100只鸡 可以有多少种买法(while循环和判断)
# x代表公鸡
x = 0
while x<=100:
#y 代表母鸡
y = 0
while y<=33:
# z代表小鸡
z = 0
while z<=100:
if (x+3*y+0.5*z == 100) and (x+y+z == 100):
print(x,y,z)
z+=1
y+=1
x+=1
0 20 80
5 19 76
10 18 72
15 17 68
20 16 64
25 15 60
30 14 56
35 13 52
40 12 48
45 11 44
50 10 40
55 9 36
60 8 32
65 7 28
70 6 24
75 5 20
80 4 16
85 3 12
90 2 8
95 1 4
100 0 0
案例三:汉诺塔递归(递归函数)
def move(n, a, buffer, c):
if(n == 1):
print(a,"->",c)
return
move(n-1, a, c, buffer)
move(1, a, buffer, c)
move(n-1, buffer, a, c)
move(3, "a", "b", "c")
a -> c
a -> b
c -> b
a -> c
b -> a
b -> c
a -> c
44.请问下面代码输出什么?
ss = 'sdfkjal;fj;lasd;asdlkf;las'
s1 = []
[s1.append(s) for s in ss if s not in s1]
print(set(s1) == set(ss))
True
var1 = "8"
var2 = "3"
var1 += var2
print('var1:',var1)
var1: 83
dictvar = {"fhzm":"张铂金","fltt":"吴玉波","jxka":"袁文娟"}
res = "张铂金" not in dictvar
print(res)
True
res = True and False
print(res)
False
res = True or False
print(res)
True
45.var = [1,{2,3},(88,99,[4,5,6,[7,8,9]])],定位四级容器中的6
var = [1,{2,3},(88,99,[4,5,6,[7,8,9]])]
var[2][2][2]
6
46.求吉利数字:100~999 : 111 222 333 444… 666…888 123 321 456 ,654 789 987…
i = 100
while i<=999:
# 个位数
gewei = i % 10
# 十位数
shiwei = i // 10 % 10
# 百位数
baiwei = i // 100
if gewei == shiwei and shiwei == baiwei :
print(i,end=' ')
if shiwei == gewei -1 and shiwei == baiwei + 1:
print(i,end=' ')
if shiwei == gewei +1 and shiwei == baiwei -1:
print(i,end=' ')
i+=1
111 123 210 222 234 321 333 345 432 444 456 543 555 567 654 666 678 765 777 789 876 888 987 999
47.如下代码输出的是什么?
def extendList(val,list=[]):
list.append(val)
return list
list1 = extendList(10)
list2 = extendList(123,[])
list3 = extendList('a')
print("list1 = %s" % list1)
print("list2 = %s" % list2)
print("list3 = %s" % list3)
list1 = [10, 'a']
list2 = [123]
list3 = [10, 'a']
def extendList(val,list=[]):
list.append(val)
return list
list1 = extendList(10)
print("list1 = %s" % list1)
list2 = extendList(123,[])
print("list2 = %s" % list2)
list3 = extendList('a')
print("list3 = %s" % list3)
list1 = [10]
list2 = [123]
list3 = [10, 'a']
def multipliers():
return [lambda x : i * x for i in range(4)]
res = multipliers()
print(res)
[<function multipliers.<locals>.<listcomp>.<lambda> at 0x0000000004B2FA60>, <function multipliers.<locals>.<listcomp>.<lambda> at 0x0000000004B2F9D8>, <function multipliers.<locals>.<listcomp>.<lambda> at 0x0000000004B2FD90>, <function multipliers.<locals>.<listcomp>.<lambda> at 0x0000000004B2F400>]
48.如果判断一个python对象的类型?
方法一:
a = 2
isinstance (a,int)
True
isinstance (a,str)
False
isinstance (a,(str,int,list)) # 是元组中的一个返回 True
True
方法二:
a = 2
print(type(a))
<class 'int'>
49.如何在一个function里面设置一个全局的变量
# 加global程序就ok了
def my_global():
global x
x=8;
my_global()
print(x)
8
50.有17个人围成一圈(编号0-16)从第0号的人开始从1报数,反报到3的倍数的人离开圈子,然后再数下去,知道最后只剩下一个人位置,问此人原来的位置是多少号?
def test(ren,count=0):
a = len(ren)
b = count
for i in ren:
b += 1
if len(ren) == 1:
return ren
if b % 3 == 0:
b += 1
ren.remove(i)
print(ren)
count += a
return test(ren,count)
ren = list(range(17))
test(ren)
[0, 1, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16]
[1, 3, 6, 7, 10, 12, 15, 16]
[3, 6, 10, 12, 16]
[3, 10, 12]
[3, 10]
[3, 10]
[10]
[10]
51.随机生成100个数,然后写入文件
思路:
- 打开一个新文件,准备去写
- 然后随机生成一个数(整数或者小数),并将该数写入文件中
- 循环第二部,知道完成100个随机数的生成
import random
with open('random.txt','a') as f:
for i in range(100):
n = random.randint(1,100)
f.write(str(n)+'\n')
52.有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去掉不满足条件的排列。
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if (i != j) and (i != k) and (j != k):
print(i,j,k)
1 2 3
1 2 4
1 3 2
1 3 4
1 4 2
1 4 3
2 1 3
2 1 4
2 3 1
2 3 4
2 4 1
2 4 3
3 1 2
3 1 4
3 2 1
3 2 4
3 4 1
3 4 2
4 1 2
4 1 3
4 2 1
4 2 3
4 3 1
4 3 2
53.如下代码执行后l,m的内容分别是什么
def func1(m):
for k,v in m.items():
m[k+2] = v+2
m = {1:2,3:4}
l = m
l[9] = 10
func1(m)
m[7] = 8
#报错:字典在遍历时不能进行修改。
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-4-4971bb0d6381> in <module>()
6 # l = m
7 # l[9] = 10
----> 8 func1(m)
9 # m[7] = 8
<ipython-input-4-4971bb0d6381> in func1(m)
1 def func1(m):
----> 2 for k,v in m.items():
3 m[k+2] = v+2
4
5 m = {1:2,3:4}
RuntimeError: dictionary changed size during iteration
54.如下代码输出的是什么
def multipliers():
return [lambda x:i * x for i in range(4)]
print(multipliers())
print([m(2) for m in multipliers()])
#考察闭包的延迟绑定
[<function multipliers.<locals>.<listcomp>.<lambda> at 0x0000000004FB8400>, <function multipliers.<locals>.<listcomp>.<lambda> at 0x0000000004FB8048>, <function multipliers.<locals>.<listcomp>.<lambda> at 0x0000000004FB82F0>, <function multipliers.<locals>.<listcomp>.<lambda> at 0x0000000004FB8378>]
[6, 6, 6, 6]