几道python 面试题

name = "lzl"
def f1():
    print(name)
def f2():
    name = 'eric'
    f1()
f2()
('hello') *3

[_ for _ in range(0,1,0.1)]
写出一个python脚本,统计文本中包含“python” 的行数(文件可能比内存大)。

num = 0
with open('a.txt', 'r', encoding='utf8') as f:
    for i in f.readline():
        if 'python' in i:
            num += 1
或者:
with open('a.txt', 'r', encoding='utf8') as f:
    for i in f:
        if 'python' in i:
            num += 1


python打开文件后,在进行读取内容时分三种情况:


(1)read() 读取整个文件,通常将文件内容放到一个字符串变量中;


(2)readline() 每次读取一行内容;


(3)readlines() 一次性读取所有内容并按行返回list;

 

 

# 截止到报错之前a,b的值是什么, 考察浅拷贝的知识
from copy import copy
def method(array):
    array.append(10)
    array[0] = 4
    array[1].append(6)
    array[2].append(8)
    array[3]['qt'] = 11
if __name__ == "__main__":
    a = [1,[2,3],(4,5,6),{'qt':10}]
    b = copy(a)
    method(b)
    print(a)
    print(b)
def append_list(num, list_num=[]):
    print(list_num)
    for i in range(num):
        list_num.append(i*i)
    print(list_num)
append_list(2)
append_list(3,[3,2,1])
append_list(3)
append_list(7)
append_list(4, [11,12,12])
qt = ['q','i', 'n','g','teng']
num = [1,2,3,4,5]
print(zip(qt,num))
a0 = dict(zip(qt,num))
print(a0)
a1 = range(8)
print(a1)
print([i+1 for i in a1 if i in a0])
a3 = [a0[s] for s in a0]
print(a3)
print([i+2 for i in a1 if i in a3])
print([i*j for i in a1 if i%2 if i> 3 for j in a0])

 

将test中的年月份识别出来:  >> ['2018年', '9月', '27日']
test = r'2018年的下半年,9月份, 不确定是不是27日'
comp = re.compile('\d+(?:\.\d+)?(?:年|月|日)')
print(comp.findall(test))

 

posted @ 2019-05-14 21:52  辣眼睛De小新  阅读(161)  评论(0编辑  收藏  举报