python入门08python_interview_question学习笔记

8.将字符串 “k:1 |k1:2|k2:3|k3:4”,处理成字典 {k:1,k1:2,…}

1
2
3
4
5
6
7
8
9
str1 = "k:1|k1:2|k2:3|k3:4"
def str2dict(str1):
dict1 = {}
for iterms in str1.split('|'):
key,value = iterms.split(':')
dict1[key] = value
return dict1
#字典推导式
d = {k:int(v) for t in str1.split("|") for k, v in (t.split(":"), )}

17.python如何实现单例模式?请写出两种实现方式?

第一种方法:使用装饰器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def singleton(cls):
instances = {}
def wrapper(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return wrapper


@singleton
class Foo(object):
pass
foo1 = Foo()
foo2 = Foo()
print(foo1 is foo2) # True

第二种方法:使用基类 New 是真正创建实例对象的方法,所以重写基类的new 方法,以此保证创建对象的时候只生成一个实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Singleton(object):
def __new__(cls, *args, **kwargs):
if not hasattr(cls, '_instance'):
cls._instance = super(Singleton, cls).__new__(cls, *args, **kwargs)
return cls._instance


class Foo(Singleton):
pass

foo1 = Foo()
foo2 = Foo()

print(foo1 is foo2) # True

第三种方法:元类,元类是用于创建类对象的类,类对象创建实例对象时一定要调用call方法,因此在调用call时候保证始终只创建一个实例即可,type是python的元类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Singleton(type):
def __call__(cls, *args, **kwargs):
if not hasattr(cls, '_instance'):
cls._instance = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instance


# Python2
class Foo(object):
__metaclass__ = Singleton

# Python3
class Foo(metaclass=Singleton):
pass

foo1 = Foo()
foo2 = Foo()
print(foo1 is foo2) # True

28.字符串 “123” 转换成 123,不使用内置api,例如 int()

方法一: 利用 str 函数

1
2
3
4
5
6
7
def atoi(s):
num = 0
for v in s:
for j in range(10):
if v == str(j):
num = num * 10 + j
return num

方法二: 利用 ord 函数

1
2
3
4
5
def atoi(s):
num = 0
for v in s:
num = num * 10 + ord(v) - ord('0')
return num

方法三: 利用 eval 函数

1
2
3
4
5
6
7
def atoi(s):
num = 0
for v in s:
t = "%s * 1" % v
n = eval(t)
num = num * 10 + n
return num

方法四: 结合方法二,使用 reduce,一行解决

1
2
3
from functools import reduce
def atoi(s):
return reduce(lambda num, v: num * 10 + ord(v) - ord('0'), s, 0)

74.一句话解决阶乘函数?

1
reduce(lambda x,y : x*y,range(1,n+1))

77.下面这段代码的输出结果将是什么?请解释。

1
2
3
def multipliers():
return [lambda x: i *x for i in range(4)]
print([m(2) for m in multipliers()])

上面代码的输出结果是[6,6,6,6],不是我们想的[0,2,4,6]

你如何修改上面的multipliers的定义产生想要的结果?

上述问题产生的原因是python闭包的延迟绑定。这意味着内部函数被调用时,参数的值在闭包内进行查找。因此,当任何由multipliers()返回的函数被调用时,i的值将在附近的范围进行查找。那时,不管返回的函数是否被调用,for循环已经完成,i被赋予了最终的值3.

1
2
3
4
5
def multipliers():
for i in range(4):
yield lambda x: i *x
def multipliers():
return [lambda x,i = i: i*x for i in range(4)]

python入门系列
python入门01廖雪峰python教程笔记
python入门02简明python教程笔记
python入门03int的四舍五入
python入门04map,numpy,pandas速查
python入门05数据可视化
python入门06标准库实例教程学习笔记
python入门07wtfbook疑问和验证
python入门08python_interview_question学习笔记

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×