Discuss / Python / 打卡 #生成器

打卡 #生成器

Topic source

JeromeYLuck

#1 Created at ... [Delete] [Delete and Lock User]

一段时间没来打卡了,加油加油!

L=[1]
while 1:
    yield L
    L = [1] + [L[x]+L[x+1] for x in range(len(L)-1)] + [1]

第二行输出为什么没有溢出?

翊翊峰情

#3 Created at ... [Delete] [Delete and Lock User]

求问这个 While 1,也就是上面内容提到的 while True的含义

yuldong

#4 Created at ... [Delete] [Delete and Lock User]

拜服这个操作

x=0时为什么列表没有溢出?此时等式右边应该是没有L[1]吧?

#杨辉三角 方案1

def triangles(): L = [1] while True: yield L L.append(0) L = [L[i-1]+L[i] for i in range(len(L))]

#杨辉三角 方案2

def triangles(): L = [1] while True: yield L L.append(0) L = [1]+[L[i-1]+L[i] for i in range(1,len(L))]

#杨辉三角 方案3

def triangles(): L=[1] while True: yield L L = [1] + [L[i-1]+L[i] for i in range(1,len(L))] +[1]

特意登陆上来为你喝彩!这个是真的牛!

没有溢出的原因是,如果list是空,根本不会进到for的代码段里去,所以根本没有执行.

Menma23132

#9 Created at ... [Delete] [Delete and Lock User]

有点没看懂,还有

##杨辉三角 方案1

def triangles(): L = [1] while True: yield L L.append(0) L = [L[i-1]+L[i] for i in range(len(L))]

##杨辉三角 方案2

def triangles(): L = [1] while True: yield L L.append(0) L = [1]+[L[i-1]+L[i] for i in range(1,len(L))]

##杨辉三角 方案3

def triangles(): L=[1] while True: yield L L = [1] + [L[i-1]+L[i] for i in range(1,len(L))] +[1]

这个方案一和二是有问题的,测试失败,用spyder输出看到了 0 list 2 [1, 0] 1 list 3 [1, 1, 0] 2 list 4 [1, 2, 1, 0] 3 list 5 [1, 3, 3, 1, 0] 4 list 6 [1, 4, 6, 4, 1, 0] 5 list 7 [1, 5, 10, 10, 5, 1, 0] 6 list 8 [1, 6, 15, 20, 15, 6, 1, 0] 7 list 9 [1, 7, 21, 35, 35, 21, 7, 1, 0] 8 list 10 [1, 8, 28, 56, 70, 56, 28, 8, 1, 0] 9 list 10 [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]


  • 1

Reply