Python如何实现单例模式?
Python有两种方式可以实现单例模式,下面两个例子使用了不同的方式实现单例模式:
第一种
class Singleton(type):
def __init__(cls, name, bases, dict):
super(Singleton, cls).__init__(name, bases, dict)
cls.instance = None
def __call__(cls, *args, **kw):
if cls.instance is None:
cls.instance = super(Singleton, cls).__call__(*args, **kw)
return cls.instance
class MyClass(object):
__metaclass__ = Singleton
print MyClass()
print MyClass()
第二种:使用decorator来实现单例模式
def singleton(cls):
instances = {}
def getinstance():
if cls not in instances:
instances[cls] = cls()
return instances[cls]
return getinstance
@singleton
class MyClass:
2:什么是lambda函数?
Python允许你定义一种单行的小函数。定义lambda函数的形式如下:labmda 参数:表达式lambda函数默认返回表达式的值。你也可以将其赋值给一个变量。lambda函数可以接受任意个参数,包括可选参数,但是表达式只有一个:
>>> g = lambda x, y: x*y
>>> g(3,4)
12
>>> g = lambda x, y=0, z=0: x+y+z
>>> g(1)
1
>>> g(3, 4, 7)
14
也能够直接使用lambda函数,不把它赋值给变量:
>>> (lambda x,y=0,z=0:x+y+z)(3,5,6)
14
如果你的函数非常简单,只有一个表达式,不包含命令,可以考虑lambda函数。否则,你还是定义函数才对,毕竟函数没有这么多限制。
3:Python是如何进行类型转换的?
Python提供了将变量或值从一种类型转换成另一种类型的内置函数。int函数能够将符合数学格式数字型字符串转换成整数。否则,返回错误信息。
>>> int(”34″)
34
>>> int(”1234ab”) #不能转换成整数
ValueError: invalid literal for int(): 1234ab
函数int也能够把浮点数转换成整数,但浮点数的小数部分被截去。
>>> int(34.1234)
34
>>> int(-2.46)
-2
函数°oat将整数和字符串转换成浮点数:
>>> float(”12″)
12.0
>>> float(”1.111111″)
1.111111
函数str将数字转换成字符:
>>> str(98)
‘98′
>>> str(”76.765″)
‘76.765′
整数1和浮点数1.0在python中是不同的。虽然它们的值相等的,但却属于不同的类型。这两个数在计算机的存储形式也是不一样。
4:如何反序的迭代一个序列?how do I iterate over a sequence in reverse order
如果是一个list, 最快的解决方案是:
list.reverse()
try:
for x in list:
“do something with x”
finally:
list.reverse()
如果不是list, 最通用但是稍慢的解决方案是:
for i in range(len(sequence)-1, -1, -1):
x = sequence[i]
如何在一个function里面设置一个全局的变量?
解决方法是在function的开始插入一个global声明:
def f()
global x
5:有两个序列a,b,大小都为n,序列元素的值任意整形数,无序;要求:通过交换a,b中的元素,使[序列a元素的和]与[序列b元素的和]之间的差最小。
将两序列合并为一个序列,并排序,为序列Source
拿出最大元素Big,次大的元素Small
在余下的序列S[:-2]进行平分,得到序列max,min
将Small加到max序列,将Big加大min序列,重新计算新序列和,和大的为max,小的为min。
Python代码
def mean( sorted_list ):
if not sorted_list:
return (([],[]))
big = sorted_list[-1]
small = sorted_list[-2]
big_list, small_list = mean(sorted_list[:-2])
big_list.append(small)
small_list.append(big)
big_list_sum = sum(big_list)
small_list_sum = sum(small_list)
if big_list_sum > small_list_sum:
return ( (big_list, small_list))
else:
return (( small_list, big_list))
tests = [ [1,2,3,4,5,6,700,800],
[10001,10000,100,90,50,1],
range(1, 11),
[12312, 12311, 232, 210, 30, 29, 3, 2, 1, 1]
]
for l in tests:
l.sort()
print “Source List:\t”, l
l1,l2 = mean(l)
print “Result List:\t”, l1, l2
print “Distance:\t”, abs(sum(l1)-sum(l2))
print ‘-*’*40
输出结果
Source List: [1, 2, 3, 4, 5, 6, 700, 800]
Result List: [1, 4, 5, 800] [2, 3, 6, 700]
Distance: 99
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Source List: [1, 50, 90, 100, 10000, 10001]
Result List: [50, 90, 10000] [1, 100, 10001]
Distance: 38
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Source List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Result List: [2, 3, 6, 7, 10] [1, 4, 5, 8, 9]
Distance: 1
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Source List: [1, 1, 2, 3, 29, 30, 210, 232, 12311, 12312]
Result List: [1, 3, 29, 232, 12311] [1, 2, 30, 210, 12312]
Distance: 21
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*