python基础之逻辑题(3)
Python基础之逻辑题(3)
如 10.3.9.12 转换规则为: 10 00001010 3 00000011 9 00001001 12 00001100 再将以上二进制拼接起来计算十进制结果: 00001010 00000011 00001001 00001100 = ? --------------------------------------------------------------------------- def func(ip): a = ip.split('.') s = '' l = [] for i in a: i = bin(int(i))[2:] # bin(10) = 0b1010 i = i.rjust(8, '0') # 0000 1010 rjust():返回一个原子符串右对齐,并使用0填充空的新字符串,如果指定的长度小于字符串的长度则返回原字符串。 l.append(i) s = s.join(l) return s ret = func('10.3.9.12 ') print(int(ret, 2)) str.rjust(width[, fillchar]) -width -- 指定填充指定字符后中字符串的总长度. -fillchar -- 填充的字符,默认为空格。 2进制 8进制 10进制 16进制 2进制 - bin(x, 8) bin(x, 10) bin(x, 16) 8进制 oct(x, 2) - oct(x, 10) oct(x, 16) 10进制 int(x, 2) int(x, 8) - int(x, 16) 16进制 hex(x, 2) hex(x, 8) hex(int(x, 10))
def num(): return [lambda x: i * x for i in range(4)] # 返回值是一个列表 print(num()) #[<function num.<locals>.<listcomp>.<lambda> at 0x00000000029726A8>, <function num.<locals>.<listcomp>.<lambda> at 0x0000000002972598>, <function num.<locals>.<listcomp>.<lambda> at 0x0000000002972730>, <function num.<locals>.<listcomp>.<lambda> at 0x00000000029727B8>] print([m(2) for m in num()]) #[6,6,6,6] def num(): return (lambda x: i * x for i in range(4)) # 返回值是一个生成器 print(num()) #<generator object num.<locals>.<genexpr> at 0x0000000002944B48> print([m(2) for m in num()]) #[0, 2, 4, 6]
a = 1 print(id(a)) def func(a): a = 2 print(id(a)) #局部变量和全局变量的地址不相同 func(a) print(a) # 1 对于a变量 在函数中定义了a=2 在函数执行完之后就销毁了 此时打印的a以旧是全局变量的a a = [] def func(a): a.append(1) func(a) print(a) #[1] 对于列表a 将a传入了函数中为a增加了一个数值 a的地址不变 #可变数据类型与不可变数据类型
num = range(2,20) for i in num: num =list(filter(lambda x:x==i or x%i ,num)) print(num) # 答案:[2, 3, 5, 7, 11, 13, 17, 19] num = range(2,20) for i in num: num =filter(lambda x:x==i or x%i ,num) print(num) print(list(num)) #<filter object at 0x000000000239C4E0> #[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
def say_hi(func): def wrapper(*args,**kwargs): print('HI') -----------------------------1 先执行 ret = func(*args,**kwargs) print('BYE')-----------------------------4 后执行 return ret return wrapper def say_yo(func): def wrapper(*args,**kwargs): print('YO')-----------------------------2 调用,所以在4之前 return func(*args,**kwargs) return wrapper @say_hi @say_yo def func(): print('rock&roll')-------------------------------3 装饰器,等2执行完在4之前 func() 结果 HI YO rock&roll BYE
my_dict ={'a':0,'b':1} def func(d): d['a'] = 1 ---------把a的值变成了1 return d func(my_dict) my_dict['c'] = 2 -----添加了一个c print(my_dict) {'a': 1, 'b': 1, 'c': 2} 字典是可变数据类型
def add_end(l=[]): l.append("end") return l add_end() # 什么都不输出 add_end() # 什么都不输出
''' data:{"time":"2016-08-05T13:13:05", "some_id":"ID1234", "grp1":{"fld1":1,"fld2":2}, "xxx2":{"fld3":0, "test":{‘fld5’:0.4}},"fld6":11,"fld7":7,"fld46":8 } fields:由"|"连接的以"fld"开头的字符串,如:fld2|fld3|fld7|fld19 def select(data,fields): return result ''' data = { "time":"1989-05-06", "some_id":123456, "grp1":{"fld1":1, "fld2":2}, "xxx2":{"fld3":0, "fld4":0.4}, "fld6":11, "fld7":7, "fld45":8 } def select(data, field): # "fld2|fld7|fld29" result = {} lst = field.split("|") for item in lst: result[item] = None for item in lst: # "fld2" if item in data: # result[item] = data[item] for value in data.values(): if type(value) == dict: d = select(value, field) for k, v in d.items(): if v: result[k] = v return result print(select(data, "fld2|fld7|fld29"))
思路: 1.任何时候两个队列总有一个是空的。 2.添加元素总是向非空队列中 add 元素。 3.取出元素的时候总是将元素除队尾最后一个元素外,导入另一空队列中,最后一个元素出队。 伪代码: class StackWithTwoQueues(object): def __init__(self): self.q1 = [] self.q2 = [] def push(self,item): self.q1.append(item) def pop(self): if len(self.q1) == 0: return None while len(self.q1) != 1: self.q2.append(self.q1.pop(0)) self.q1,self.q2 = self.q2,self.q1 return self.q2.pop(0)
class Parent(object): x = 1 class Child1(Parent): pass class Child2(Parent): pass print(Parent.x,Child1.x,Child2.x) #1,1,1 Child1.x = 2 print(Parent.x,Child1.x,Child2.x) #1,2,1 Parent.x = 3 print(Parent.x,Child1.x,Child2.x) #3,2,3 #当子类有值的时候不继承父类,想继承用super()