python面试笔试题

一、前言:两年没面试了,今天出去试了试市场行情,顺便看看现在公司要求的技能。今天写了几个题,答得还算可以,笔试题不难都是技术,但是很考验功底,贴别是python基本语法和常见的操作。今天的几个题也是很有意思的。后面会一直更新我遇到的python面试题。
二·、面试题:
1.xxxx公司(2020.10.29):
(1)面试题:
在这里插入图片描述
在这里插入图片描述

(2)答案:

print(int(1.1))
print(int(1.9))

a = [1,2,3]
b = [3,4,5]
print(a+b)
print(set(a+b))

text = 'AbCdefG'
print(text.lower())

# 1-50奇数  51-100偶数求和
unevenNum = 0
evenNum = 0
for i in range(1,101):
    if i%2 != 0 and i<51:
        unevenNum+=i
    elif i%2 == 0 and i<101:
        evenNum+=i
print(unevenNum+evenNum)

a = 'hgaabcdef'
b = 'zxydaqr'
c = set(list(a+b))
s="".join((lambda x:(x.sort(),x)[1])(list(c)))
print(s)

# -123 -321
a = -123
c = -1*int(str(abs(a))[::-1])
print(c)
1
1
[1, 2, 3, 3, 4, 5]
{
    
    1, 2, 3, 4, 5}
abcdefg
3175
abcdefghqrxyz
-321

2.xxx公司面试题(2020.11.24)
(1)笔试题:
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
(2)答案:

  1. 一种更高级的元组叫 namedtuple ,《流畅的Python》上把它翻译为具名元组,顾名思义,就是带有名字的元组。namedtuple类位于collections模块,有了namedtuple后通过属性访问数据能够让我们的代码更加的直观更好维护。
    namedtuple能够用来创建类似于元祖的数据类型,除了能够用索引来访问数据,能够迭代,还能够方便的通过属性名来访问数据。
    定义 namedtuple 时,第一个参数就是元组的名字,这里很像我们自定义类中的类名,第二个参数是用空格隔开的字符串(也可以是字符串组成的列表),代表元组中的4个字段,相当于类中的4个属性。初始化方式和类的实例对象是一样的。
from collections import namedtuple
 
Friend=namedtuple("Friend",['name','age','email'])

f1=Friend('xiaowang',33,'[email protected]')
print(f1)
print(f1.age)
print(f1.email)
print(f1[0])

f2=Friend(name='xiaozhang',email='[email protected]',age=30)
print(f2)
name,age,email=f2
print(name,age,email)
[root@localhost demo01]# python3 text1124.py 
Friend(name='xiaowang', age=33, email='[email protected]')
33
xiaowang@163.com
xiaowang
Friend(name='xiaozhang', age=30, email='[email protected]')
xiaozhang 30 xiaozhang@sina.com

  1. python函数的参数类型可分为:必须参数、默认参数、可变参数(不定长参数)、关键字参数、组合参数,总共5种.
print(round(1.23456, 2))
print(round(1627731, -1))
print(round(6,-1))
print(round(5,-1))
print(round(4,-1))
print(round(80,-2))
print(round(40,-2))
[root@localhost demo01]# python3 text1124.py 
1.23
1627730
10
0
0
100
0

x = 10
a = lambda y: x+y
x=20
b=lambda y: x+y

print(a(10)) #1
print(b(10)) #2
[root@localhost demo01]# python3 text1124.py 
30
30

lambda 的参数,在运行时绑定值,而不是定义时就绑定,这跟函数的默认值参数定义是不同的。Python lambda介绍

prices = {
    
    
    'a': 45.23,
    'b': 612.78,
    'c': 205.55,
    'd': 10.75
}
min_prcice = min(zip(prices.values(), prices.keys()))
max_prcice = max(zip(prices.values(), prices.keys()))
prices_sorted = sorted(zip(prices.values(), prices.keys()))
print(min_prcice)
print(max_prcice)
print(prices_sorted)
[root@localhost demo01]# python3 text1124.py 
(10.75, 'd')
(612.78, 'b')
[(10.75, 'd'), (45.23, 'a'), (205.55, 'c'), (612.78, 'b')]

a=[1,5,2,1,9,1,5,10]
setlist = list(set(a))
setlist.sort(key=a.index)
print(setlist)

注意:两个字典如果不考虑键相同则相加的话,可以使用d1.update(d2)可以很方便合并,但这样的后面的字典到中的值会覆盖字典d1中的值。

for key, value in b.items():
    if key in a:
        a[key] += value
    else:
        a[key] = value
print(a)
  1. 找到两个文件的共有行:
alines = open('a.txt','r').readlines()
fw = open('c.txt','w')
for line in open('b.txt','r'):
    for al in alines:
        if al[:-1] in line:
            fw.write(line)
 
fw.close()

猜你喜欢

转载自blog.csdn.net/qq_34663267/article/details/109363161