*)可以这样,不用保存递归中的变量
import os
def findFile (str,dir = os.path.abspath('.')):
for x in os.listdir(dir):
if os.path.isdir(os.path.join(dir,x)):
findFile(str,os.path.join(dir,x))
elif str in x:
print(os.path.join(dir,x))#我一直都是想办法保存在递归的程序中
*)谁说while最少会执行一次的,并不是这样
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
>>> while a>2: ... print(a) ... a-=1 ... >>> >>> a=3 >>> while a>2: ... print(a) ... a-=1 ... 3
*)append()和extend()的区别
append()和extend()都只能接受一个参数,但append()能接受不可迭代的或者不可迭代的,但extend只能接受可迭代的(iterable)
>>> a.extend(2,3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: extend() takes exactly one argument (2 given)
>>> a.extend(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
append()把参数添加到一个下标内
>>> a.append(b)
>>> a
[1, 2, 2, 4, [2, 4]]
extend()不是
>>> a=[1,2]
>>> b=[2,4]
>>> a.extend(b)
>>> a
[1, 2, 2, 4]
>>> a.append(b)
>>> a
[1, 2, 2, 4, [2, 4]]
*)方法名相同的情况下,例如方法名内部有重名的方法和参数,调用的情况
def name1(collection):
print('外面的name1,参数:collection:',collection)
def name1(collection):
print('里面的name1,参数:collection:',collection)
name1(collection)
if __name__=='__main__':
collection=[1,2,3,4,5,6]
name1(collection[2:])
(sort) λ python forTest.py
外面的name1,参数:collection: [3, 4, 5, 6]
里面的name1,参数:collection: [3, 4, 5, 6]
*)递归失败:
def name1(collection):
print('外面的name1,参数:collection:',collection)
name1(collection)
Traceback (most recent call last):
File "forTest.py", line 8, in <module>
name1(a)
File "forTest.py", line 5, in name1
name1(collection)
File "forTest.py", line 5, in name1
name1(collection)
File "forTest.py", line 5, in name1
name1(collection)
[Previous line repeated 993 more times]
File "forTest.py", line 2, in name1
print('外面的name1,参数:collection:',collection)
RecursionError: maximum recursion depth exceeded while calling a Python object
*)python中的切片也是[a:b]是从a到b-1的
*)关于for循环中range(2),i到底是从0还是1开始。特别是在用数组的长度作为range的参数的时候经常会犯糊涂
还有range(a,b,c)无论怎样,返回的数组都是[a,....b-1](c>0)或者[a,.....b+1](c<0)就是不到b
#首先
>>> for i in range(5):
... print(i)
...
0
1
2
3
4
#其次
>>> s=[1,2,3,4,5]
>>> length=len(s)
>>> for i in range(length):#所以,这里完全不用-1,类似于,因为range()会减去1,这就抵消掉了数组长度比数组下标多了1这个属性说造成的访问数组会超出index这个trouble。
... print(s[i])
...
1
2
3
4
5
>>> length
5
*)range反向循环、反向递减、将步长设置为负数就好了,注意要调换开始和结束的位置
>>> for i in range(5,3,-1):#从5开始,到3结束
... print(i)
...
5
4
>>>
*)这样range(0,0)并不会抛出异常,而是什么也不输出
>>> for i in range(0,0):
... print(i)
...
>>>