2019-09-17 01:03:28 +08:00
|
|
|
|
## Python面试题
|
|
|
|
|
|
|
|
|
|
1. 说一说Python中的新式类和旧式类有什么区别。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
2. Python中`is`运算符和`==`运算符有什么区别?
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:请参考[《那些年我们踩过的那些坑》](../番外篇/那些年我们踩过的那些坑.md)。
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
3. Python中如何动态设置和获取对象属性?
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:`setattr(object, name, value)`和`getattr(object, name[, default])`内置函数,其中`object`是对象,`name`是对象的属性名,`value`是属性值。这两个函数会调用对象的`__getattr__`和`__setattr__`魔术方法。
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
4. Python如何实现内存管理?有没有可能出现内存泄露的问题?
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
5. 阐述列表和集合的底层实现原理。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
2019-09-22 23:52:42 +08:00
|
|
|
|
6. 现有字典`d = {'a': 24, 'g': 52, 'i': 12, 'k': 33}`,如何按字典中的值对字典进行排序得到排序后的字典。
|
|
|
|
|
|
|
|
|
|
答:
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
|
|
|
|
|
```
|
2019-09-17 01:03:28 +08:00
|
|
|
|
|
|
|
|
|
7. 实现将字符串`k1:v1|k2:v2|k3:v3`处理成字典`{'k1': 'v1', 'k2': 'v2', 'k3': 'v3'}`。
|
|
|
|
|
|
2019-09-22 23:52:42 +08:00
|
|
|
|
答:
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
{key: value for key, value in (
|
|
|
|
|
item.split(':') for item in 'k1:v1|k2:v2|k3:v3'.split('|')
|
|
|
|
|
)}
|
|
|
|
|
```
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
8. 写出生成从`m`到`n`公差为`k`的等差数列的生成器。
|
|
|
|
|
|
2019-09-22 23:52:42 +08:00
|
|
|
|
答:
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
(value for value in range(m, n + 1, k))
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
或
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
def generate(m, n, k):
|
|
|
|
|
for value in range(m, n + 1, k):
|
|
|
|
|
yield value
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
或
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
def generate(m, n, k):
|
|
|
|
|
yield from range(m, n + 1, k)
|
|
|
|
|
```
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
9. 请写出你能想到的反转一个字符串的方式。
|
|
|
|
|
|
2019-09-22 23:52:42 +08:00
|
|
|
|
答:
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
''.join(reversed('hello'))
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
'hello'[::-1]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
def reverse(content):
|
|
|
|
|
return ''.join(content[i] for i in range(len(content) - 1, -1, -1))
|
|
|
|
|
|
|
|
|
|
reverse('hello')
|
|
|
|
|
```
|
|
|
|
|
|
2019-09-20 00:25:24 +08:00
|
|
|
|
```Python
|
2019-09-22 23:52:42 +08:00
|
|
|
|
def reverse(content):
|
|
|
|
|
return reverse(content[1:]) + content[0] if len(content) > 1 else content
|
|
|
|
|
|
|
|
|
|
reverse('hello')
|
2019-09-20 00:25:24 +08:00
|
|
|
|
```
|
2019-09-17 01:03:28 +08:00
|
|
|
|
|
2019-09-22 23:52:42 +08:00
|
|
|
|
10. 不使用任何内置函数,将字符串`'123'`转换成整数`123`。
|
|
|
|
|
|
|
|
|
|
答:
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
nums = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
|
|
|
|
|
total = 0
|
|
|
|
|
for ch in '123':
|
|
|
|
|
total *= 10
|
|
|
|
|
total += nums[ch]
|
|
|
|
|
print(total)
|
|
|
|
|
```
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
11. 写一个返回bool值的函数,判断给定的非负整数是不是回文数。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
12. 用一行代码实现求任意非负整数的阶乘。
|
|
|
|
|
|
2019-09-22 23:52:42 +08:00
|
|
|
|
答:
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
from functools import reduce
|
|
|
|
|
|
|
|
|
|
(lambda num: reduce(int.__mul__, range(2, num + 1), 1))(5)
|
|
|
|
|
```
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
13. 写一个函数返回传入的整数列表中第二大的元素。
|
|
|
|
|
|
2019-09-22 23:52:42 +08:00
|
|
|
|
答:
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
14. 删除列表中的重复元素并保留原有的顺序。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
15. 找出两个列表中的相同元素和不同元素。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
16. 列表中的某个元素出现次数占列表元素总数的半数以上,找出这个元素。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
17. 实现对有序列表进行二分查找的算法。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
18. 输入年月日,输出这一天是这一年的第几天。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
19. 统计一个字符串中各个字符出现的次数。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
20. 在Python中如何实现单例模式?
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
21. 下面的代码会输出什么。
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
class A:
|
|
|
|
|
|
|
|
|
|
def __init__(self, value):
|
|
|
|
|
self.__value = value
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def value(self):
|
|
|
|
|
return self.__value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
a = A(1)
|
|
|
|
|
a.__value = 2
|
|
|
|
|
print(a.__value)
|
|
|
|
|
print(a.value)
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
22. 实现一个记录函数执行时间的装饰器。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
23. 写一个遍历指定目录下指定后缀名的文件的函数。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
24. 有如下所示的字典,请将其转换为CSV格式。
|
|
|
|
|
|
|
|
|
|
转换前:
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
dict_corp = {
|
|
|
|
|
'cn': {'id': 1, 'name': '土豆', 'desc': '土豆', 'price': {'gold': 20, 'kcoin': 20}},
|
|
|
|
|
'en': {'id': 1, 'name': 'potato', 'desc': 'potato', 'price': {'gold': 20, 'kcoin': 20}},
|
|
|
|
|
'kr': {'id': 1, 'name': '감자', 'desc':'감자', 'price': {'gold': 20, 'kcoin': 20}},
|
|
|
|
|
'jp': {'id': 1, 'name': 'ジャガイモ', 'desc': 'ジャガイモ', 'price': {'gold': 20, 'kcoin': 20}},
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
转换后:
|
|
|
|
|
|
|
|
|
|
```CSV
|
|
|
|
|
,id,name,desc,gold,kcoin
|
|
|
|
|
cn,1,土豆,土豆,20,20
|
|
|
|
|
en,1,potato,potato,20,20
|
|
|
|
|
kr,1,감자,감자,20,20
|
|
|
|
|
jp,1,ジャガイモ,ジャガイモ,20,20
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
25. 有如下所示的日志文件,请用Python程序或Linux命令打印出独立IP并统计数量。
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
221.228.143.52 - - [23/May/2019:08:57:42 +0800] ""GET /about.html HTTP/1.1"" 206 719996
|
|
|
|
|
218.79.251.215 - - [23/May/2019:08:57:44 +0800] ""GET /index.html HTTP/1.1"" 206 2350253
|
|
|
|
|
220.178.150.3 - - [23/May/2019:08:57:45 +0800] ""GET /index.html HTTP/1.1"" 200 2350253
|
|
|
|
|
218.79.251.215 - - [23/May/2019:08:57:52 +0800] ""GET /index.html HTTP/1.1"" 200 2350253
|
|
|
|
|
219.140.190.130 - - [23/May/2019:08:57:59 +0800] ""GET /index.html HTTP/1.1"" 200 2350253
|
|
|
|
|
221.228.143.52 - - [23/May/2019:08:58:08 +0800] ""GET /about.html HTTP/1.1"" 206 719996
|
|
|
|
|
221.228.143.52 - - [23/May/2019:08:58:08 +0800] ""GET /news.html HTTP/1.1"" 206 713242
|
|
|
|
|
221.228.143.52 - - [23/May/2019:08:58:09 +0800] ""GET /products.html HTTP/1.1"" 206 1200250
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
26. 请写出从HTML页面源代码中获取a标签href属性的正则表达式。
|
|
|
|
|
|
2019-09-22 23:52:42 +08:00
|
|
|
|
答:
|
|
|
|
|
|
|
|
|
|
```Python
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
27. 正则表达式对象的`search`和`match`方法有什么区别?
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
28. 当做个线程竞争一个对象且该对象并非线程安全的时候应该怎么办?
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
29. 说一下死锁产生的条件以及如何避免死锁的发生。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
30. 请阐述TCP的优缺点。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
31. HTTP请求的GET和POST有什么区别?
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
32. 说一些你知道的HTTP响应状态码。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
33. 简单阐述HTTPS的工作原理。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
34. 阐述Django项目中一个请求的生命周期。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
35. Django项目中实现数据接口时如何解决跨域问题。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
36. Django项目中如何对接Redis高速缓存服务。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
37. 请说明Cookie和Session之间的关系。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
2019-09-17 01:03:28 +08:00
|
|
|
|
38. 说一下索引的原理和作用。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
2019-09-30 01:32:16 +08:00
|
|
|
|
39. 是否使用过Nginx实现负载均衡?用过哪些负载均衡算法?
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
2019-09-30 01:32:16 +08:00
|
|
|
|
40. 一个保存整数(int)的数组,除了一个元素出现过1次外,其他元素都出现过两次,请找出这个元素。
|
|
|
|
|
|
2019-12-12 18:11:34 +08:00
|
|
|
|
答:
|
|
|
|
|
|
|
|
|
|
41. 有12个外观相同的篮球,其中1个的重要和其他11个的重量不同(有可能轻有可能重),现在有一个天平可以使用,怎样才能通过最少的称重次数找出这颗与众不同的球。
|
|
|
|
|
|
|
|
|
|
答:
|