python相关代码面试题
2019-03-15 本文已影响0人
探索1者
# 输出每个字母出现的次数
s1 = ' aaaabbbbcccddddaa '
s2 = ''
for i in range(len(s1)):
if s1[i] in s2:
continue
count = 0
for j in range(i,len(s1)):
if s1[i] == s1[j]:
count += 1
s2 += s1[i] + str(count)
print(s2)
def additem(listP):
listP + = [1]
mylist = [1,2,3,4]
additem(mylist)
k = len(mylist)
print(mylist) # [1,2,3,4,1]
print(k) #5
#=========================================================#
counter = 0
def doThings():
global counter
for i in (1,2,3):
counter += 1
doThings()
print(counter)# 6
print('==========+++++++++++++===========')
class Person:
def __init__(self,id):
self.id = id
jack = Person(1)
jack.__dict__['age']=22
oo = jack.age + len(jack.__dict__)
print(jack.__dict__)# {'id':1,'age':22}
print(oo)# 22+2
# 魔法函数 __doc__属性
def foo():
"This is cool simple function that return 1"
return 1
print(foo.__doc__[-8:-2])
#===================================================#
class A:
def __init__(self,a,b,c):
self.x = a+b+c
a = A(1,2,3)
b = getattr(a,'x')
print(b) # 6
setattr(a,'x',b+1)
print(a.x)# 7