阿里面试总结

本文深入探讨了Python中的关键概念和技术,包括字符串操作、正则表达式、多线程与多进程处理、内存管理及垃圾回收机制等内容,并介绍了mqtt协议的基本特性和应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Python

字符串匹配,match,和search

参考

Python里面search()和match()的区别?

match只检测是不是在字符串的开始位置匹配。search会扫描整个字符串进行匹配,返回第一个匹配。

>>> import re
>>> re.match('super', 'supervisor')
<_sre.SRE_Match object at 0x108b5b168>
>>> re.match('super', 'supervisor').span()
(0, 5)
>>> print re.match('super', 'isupervisor')
None
>>> re.search('super','isupervisor')
<_sre.SRE_Match object at 0x108b5b168>
>>> re.search('super','isupervisor').span()
(1, 6)

GIL(Global Interpreter Lock)

http://blog.youkuaiyun.com/waltonhuang/article/details/52045215

  1. 不是Python的特性,是解释器CPython的特性。
  2. 是一个全局锁,一个Mutex。线程只有获得了这个锁才能执行机器码。不同的线程之间其实是分时执行。
  3. 在单核下性能接近单线程。多核下表现糟糕。主要是因为锁的竞争。锁的持有者释放后又马上获得。
  4. 解决办法可以用多进程,可以使用其他语言实现CPU密集型的计算,可以用新版本的Python。新版本的Python使用基于时间片的切换,还能保证刚释放锁的线程不能马上再获得锁。

字符串替换

http://www.cnpythoner.com/post/65.html

直接替换,用replace

>>> a = 'hello world hello'
>>> a.replace('hello', 'bye')
'bye world bye'

正则表达式替换,用sub

>>> myre = re.compile('h[a-z]+o')
>>> myre.sub('bye', a)
'bye world bye'

编码问题

http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386819196283586a37629844456ca7e5a7faa9b94ee8000

  1. 一开始只有ASCII码。
  2. 后来为了支持不同的语言,出现了Unicode。
  3. 但是Unicode太长了,为了节省,出现了UTF-8。
  4. 计算机内存中,统一使用Unicode编码。传输的时候、存储的时候使用UTF-8节省空间。
  5. UTF-8转Unicode使用decode,Unicode转UTF-8使用encode。
>>> u'abc'.encode('utf8')
'abc'
>>> u'中文'.encode('utf8')
'\xe4\xb8\xad\xe6\x96\x87'
>>> 'abc'.decode('utf8')
u'abc'
>>> '\xe4\xb8\xad\xe6\x96\x87'.decode('utf8')
u'\u4e2d\u6587'
>>> print '\xe4\xb8\xad\xe6\x96\x87'.decode('utf8')
中文

map-reduce

http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00141861202544241651579c69d4399a9aa135afef28c44000

是函数式编程。

map()把函数作用到每一个对象中。

def f(x):
    return x * x
print map(f, [1,2,3,4,5])
# [1, 4, 9, 16, 25]
def normal(str):
    head = str[0]
    tail = str[1:]
    return head.upper() + tail.lower()
print map(normal, ['a', 'A', 'aLiCE'])
# ['A', 'A', 'Alice']

reduce()把一个函数作用在一个序列上,这个函数必须接收两个参数,reduce把作用的结果继续和序列的下一个元素做函数的操作。

def f2(x, y):
    return x*y
print reduce(f2, [1,2,3,4])

或者使用lambda函数

>>> reduce(lambda x,y:x*y, [1,2,3,4])
24

混合使用,str2int

def char2num(c):
    return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9':9}[c]

print reduce(lambda x,y: x*10+y, map(char2num, '12345'))
# 12345

future

http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386820023084e5263fe54fde4e4e8616597058cc4ba1000

可以在旧版本的Python中引入一些新版本Python的特性,以供测试,测试稳定了再迁移到高版本去。
如3.x中所有字符串被视为Unicode,地板除。

用了什么包

  • MySQLdb
  • tornado
  • xlrd
  • xlwt
  • matplotlib
  • sklearn
  • re
  • beautifulsoup Python写的HTML/XML解释器

内存管理,垃圾回收

Garbage Collection for Python
译文:
http://blog.youkuaiyun.com/waltonhuang/article/details/52056173

Python Garbage Collection
译文:
http://blog.youkuaiyun.com/waltonhuang/article/details/52055380

Garbage Collector interface

Python深入06 Python的内存管理(里面引用环的图不错,可以帮助理解)

Python垃圾回收机制:gc模块

Python 源码阅读——垃圾回收机制

Python垃圾回收机制:gc模块(有很好的代码示例和说明)

  1. 在Python中,每个对象都有一个引用计数。引用计数降为0的时候就回收该对象。
  2. 但是如果出现孤立引用环的情况则无法使用引用计数,此时需要使用一种与传统的标记-清除相反的算法来找出所有不可访问的对象。
  3. 分代回收技术。所有对象分为0,1,2三代。存活时间越长的对象,垃圾回收的扫描频率越低。
  4. 还有一个麻烦的finalizer问题。循环引用的两个对象都有__del__的时候先调用哪个?调用了第一个对象的finalizer之后还不能释放这个对象,因为第二个finalizer还可以访问到第一个对象。Python把这种情况放入到一个全局的不可回收垃圾列表。程序员需要找出一种在应用中有意义的方法来回收这些对象。

匿名函数

就是lambda函数。

关键字lambda表示匿名函数,冒号前面为函数参数,冒号后面为返回值。

多进程多线程, master-worker

http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832973658c780d8bfa4c6406f83b2b3097aed5df6000

  1. 多进程使用mutiprocessing模块,可以使用Queue,Pipe等实现进程间通信
  2. 多线程使用threading模块。
  3. 多线程和多进程最大的不同是,多进程的变量是独享的,互不影响;而多线程的变量是共享的,任何一个变量都可能被任何一个线程所改变。多线程最大的危险就是多个线程同时修改一个变量,把变量改乱了。(可以通过加锁来保护)(try…finally)
  4. 多任务通常以Master-Worker的方式实现,Master负责分配工作,Worker负责执行工作。多进程和多线程都能实现。但是多进程要稳定一些,因为子进程挂了其他进程还能继续工作,但是多线程中一个挂了就全挂了。
  5. 分布式进程可以通过mutiprocessing.manager实现,把一个Queue注册到网络上进行通信。

2.mqtt

MQTT协议简记

  1. 发布/订阅模型
  2. 基于TCP/IP
  3. 支持QoS

QoS

订阅者和发布者的QoS不一样的时候按低的来。
http://www.cnblogs.com/sunxucool/p/5439779.html

Downgrade of QoS

As already said, the QoS flows between a publishing and subscribing client are two different things as well as the QoS can be different. That means the QoS level can be different from client A, who publishes a message, and client B, who receives the published message. Between the sender and the broker the QoS is defined by the sender. When the broker sends out the message to all subscribers, the QoS of the subscription from client B is used. If client B has subscribed to the broker with QoS 1 and client A sends a QoS 2 message, it will be received by client B with QoS 1. And of course it could be delivered more than once to client B, because QoS 1 only guarantees to deliver the message at least once.

官网说明

Assuming that the requested QoS level is granted, the client receives PUBLISH messages at less than or equal to this level, depending on the QoS level of the original message from the publisher. For example, if a client has a QoS level 1 subscription to a particular topic, then a QoS level 0 PUBLISH message to that topic is delivered to the client at QoS level 0. A QoS level 2 PUBLISH message to the same topic is downgraded to QoS level 1 for delivery to the client.

A corollary to this is that subscribing to a topic at QoS level 2 is equivalent to saying “I would like to receive messages on this topic at the QoS at which they are published”.

This means a publisher is responsible for determining the maximum QoS a message can be delivered at, but a subscriber is able to downgrade the QoS to one more suitable for its usage. The QoS of a message is never upgraded.

订阅者把QoS设置为2,意思就是说发布者发的是什么QoS就按什么QoS来了。

session

还是参考MQTT官方文档

clean session flag
如果设置为0,那么服务器会在客户端断开连接之后保存它的订阅。这包括了继续存储被订阅的主题的QoS1和QoS2的消息,那么客户端重新连接的时候这些信息就可以被传到客户端了。服务器也必须维护在传输路上的消息。
如果设置为1,服务器就丢弃所有之前维护的信息,认为连接是“干净的”。

还有paho的文档

The Session state in the Client consists of:
· QoS 1 and QoS 2 messages which have been sent to the Server, but have not been completely acknowledged.
· QoS 2 messages which have been received from the Server, but have not been completely acknowledged.

The Session state in the Server consists of:
· The existence of a Session, even if the rest of the Session state is empty.
· The Client’s subscriptions.
· QoS 1 and QoS 2 messages which have been sent to the Client, but have not been completely acknowledged.
· QoS 1 and QoS 2 messages pending transmission to the Client.
· QoS 2 messages which have been received from the Client, but have not been completely acknowledged.
· Optionally, QoS 0 messages pending transmission to the Client.

persistent session

http://www.hivemq.com/blog/mqtt-essentials-part-7-persistent-session-queuing-messages

persistent sesson支持会话的状态的保持,在broker保存了所有与客户端相关的信息。用什么来标识?是用客户端建立连接时的指定的clientId。

主题匹配

http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#appendix-a
译文:
http://blog.youkuaiyun.com/waltonhuang/article/details/52066908

层次分割符/
匹配多层#,只能用在最后。
匹配一层+,可以用在最后也可以用在中间。

客户端

broker

3.mysql

设计数据表

4.项目

最大收获

机器学习算法

发明和专利

复制出新项目,还怎么版本管理?

5.linux

用过什么命令

查看日志文件,从大量的文件中查找。找访问ip的统计。

http://blog.youkuaiyun.com/waltonhuang/article/details/52083868

6.前端

jquery

7.还有什么问题

其他

不要老说好像。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值