笔试基础题

__init__括号中的变量是实例变量,里面定义的是默认的,但实例可以传参数对象进去

下方输出任然是123

class a:
    def __init__(self,id):
        self.id = id
        id = 100
a = a(123)
print(a.id) # 123

continue跳出当次的循环,本例是跳出为偶数的循环

n = 0
while n < 10:
    n +=1
    if n % 2 == 0:
        continue
    print(n)

zip加dict实现两个有序序列的组合成字典

a = dict(zip(('a','b','c','d','e'),(1,2,3,4,5)))
print(a) # {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}
b = list(range(10))
for i in a:
    print('打印出来的是字典的键',i)
print([i for i in b if i in a])

类的实例化传入相同的参数,形成的实例的id并不相同,但类型相同

class D(object):
    def __init__(self,n):
        self.name = n
    
a = D('1')
b = D('1')
c = D('2')
print(type(a)==type(c)) # True
print(id(a)==id(b)) # False

元组或列就算只有一个参数,也要加逗号(,)

a = (1)
b = [1]
print(type(a)) # <class 'int'>
print(type(1)) # <class 'int'>
print('ss'=='ss')

re模块中的findall会提取出单引号'',代表字符串

提取出代字符串的参数,但遍历时候就没有了

import re
t = 'Comuputer says "no." Phone says "yes."'
s = re.compile(r'\"(.*?)\"') # 
a1 = s.findall(t)
print(a1,type(a1))

for i in list(a1):
    print(i,type(i)) # no. <class 'str'>
print(s.findall(t)) # ['no.', 'yes.']

.items()返回个dict_items()可迭代对象

a = {'x':1,'y':2,'z':3}
b = {'w':10,'x':11,'y':2}
print(a.items() & b.items()) # {('y', 2)}
print(a.items() ) # dict_items([('x', 1), ('y', 2), ('z', 3)])
print(b.items()) # dict_items([('w', 10), ('x', 11), ('y', 2)])
posted @ 2022-03-09 16:48  索匣  阅读(15)  评论(0编辑  收藏  举报