1、
简单让A、B的值交换
a,b = 1,2 print(a,b) a = 1 b = 2 a,b = b,a print(a,b)
a,b=[1,2],[5,9] print(a,b)
2、元组 如果元祖里面只有一个元素且不加,那此元素是什么类型,就是什么类型。
tu1 = (1) tu2 = (1,) print(tu1,type(tu1)) print(tu2,type(tu2)) tu1 = ([1]) tu2 = ([1],) print(tu1,type(tu1)) print(tu2,type(tu2))
3、
l1 = [1,[1],2,3,4] l2 = l1[:] l1[1].append('a') #l2 的结果是什么? #l1 和l2同时都是[1, [1, 'a'], 2, 3, 4] 浅copy print(l1,id(l1)) print(l2,id(l2)) print(l1[1] is l2[1])
生成器相关面试题:
一、
def demo(): for i in range(4): yield i g=demo() g1=(i for i in g) g2=(i for i in g1) print(list(g1)) print(list(g2)) 面试题1
二、
def add(n,i): return n+i def test(): for i in range(4): yield i g=test() for n in [1,10]: g=(add(n,i) for i in g) print(list(g)) 面试题2
三、
import os def init(func): def wrapper(*args,**kwargs): g=func(*args,**kwargs) next(g) return g return wrapper @init def list_files(target): while 1: dir_to_search=yield for top_dir,dir,files in os.walk(dir_to_search): for file in files: target.send(os.path.join(top_dir,file)) @init def opener(target): while 1: file=yield fn=open(file) target.send((file,fn)) @init def cat(target): while 1: file,fn=yield for line in fn: target.send((file,line)) @init def grep(pattern,target): while 1: file,line=yield if pattern in line: target.send(file) @init def printer(): while 1: file=yield if file: print(file) g=list_files(opener(cat(grep('python',printer())))) g.send('/test1') 协程应用:grep -rl /dir tail&grep
内置函数相关的面试题:
现有两个元组(('a'),('b')),(('c'),('d')),请使用python中匿名函数生成列表[{'a':'c'},{'b':'d'}]
#答案一 test = lambda t1,t2 :[{i:j} for i,j in zip(t1,t2)] print(test(t1,t2)) #答案二 print(list(map(lambda t:{t[0]:t[1]},zip(t1,t2)))) #还可以这样写 print([{i:j} for i,j in zip(t1,t2)]) coding
1.下面程序的输出结果是: d = lambda p:p*2 t = lambda p:p*3 x = 2 x = d(x) x = t(x) x = d(x) print x 2.现有两元组(('a'),('b')),(('c'),('d')),请使用python中匿名函数生成列表[{'a':'c'},{'b':'d'}] 3.以下代码的输出是什么?请给出答案并解释。 def multipliers(): return [lambda x:i*x for i in range(4)] print([m(2) for m in multipliers()]) 请修改multipliers的定义来产生期望的结果。 练习
第三题答案:
使用递归函数实现斐波那契数列:
def func(n,a=1,b=1): if n == 1 : return a return func(n-1,b,a+b) print(func(100))
使用递归实现阶乘算法:
def fac(n): if n == 1 : return 1 return n * fac(n-1) print(fac(10))
纸牌游戏(《流畅的Python》)
l
View Code
import collections from random import choice import random Card = collections.namedtuple('Card',['rank','suit']) class FrenchDeck: ranks = [str(n) for n in range(2,11)] + list('JQKA') suits = 'spades diamonds clubs hearts'.split() #黑桃 方片 梅花 红桃 def __init__(self): self.cards = [Card(rank,suit) for suit in self.suits for rank in self.ranks] def __len__(self): return len(self.cards) def __getitem__(self, position): return self.cards[position] beer_card = Card('7','diamonds') #方片 7 print(beer_card) deck = FrenchDeck() print(deck.__dict__) print(len(deck)) print(deck[0]) print(deck[-1]) print(choice(deck)) #对应 from random import choice n = random.choice(deck) print(n) print(deck[:3]) print(deck[12::13]) for card in deck: print(card) for card in reversed(deck): print(card) print(Card('Q','hearts') in deck) print(Card('7','beasts') in deck) from random import shuffle #随机洗牌 shuffle(deck)
去重题型
class Person: def __init__(self,name,age,sex): self.name = name self.age = age self.sex = sex def __hash__(self): return hash(self.name+self.sex) def __eq__(self, other): if self.name == other.name and self.sex == other.sex:return True p_lst = [] for i in range(84): p_lst.append(Person('egon',i,'male')) print(p_lst) print(set(p_lst)) 一道面试题