题型分析

1、

```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))

```def add(n,i):
return n+i

def test():
for i in range(4):
yield i

g=test()
for n in [1,10]:

print(list(g))

```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')

tail&grep```

内置函数相关的面试题：

```#答案一
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()])

使用递归函数实现斐波那契数列：

```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》）

```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))

74 篇文章24 人订阅

0 条评论

相关文章

数据结构和算法——旋转打印链表

1、问题描述 输入参数nnn为正整数，如输入n=5n=5n=5，则按行打印如下的数字： ? 2、问题的理解 这个问题是将数字1…n21…n21\dots n^2...

2993

2057

1374

SQL递归查询(with cte as)

递归CTE最少包含两个查询(也被称为成员)。第一个查询为定点成员，定点成员只是一个返回有效表的查询，用于递归的基础或定位点。第二个查询被称为递归成员，使该查...

751

793

3878

9972

1081

iOS weak的实现原理

weak表其实是一个hash表，Key是所指对象的地址，Value是weak指针的地址数组，weak是弱引用，所引用对象的计数器不会+1,并在引用对象被释放的时...

2365

[十一]JavaIO之DataInputStream 和 DataOutputStream

DataInputStream和DataOutputStream 继承了各自的FilterInputStream以及FilterOutputStream

992