學習Python 中經常遇到疑惑的地方

2021-10-01 07:37:04 字數 3871 閱讀 5435

可以這樣,不用儲存遞迴中的變數

import os

deffindfile

(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

strin x:

print

(os.path.join(

dir,x)

)#我一直都是想辦法儲存在遞迴的程式中

誰說while最少會執行一次的,並不是這樣

>>

>

while a>2:

...print

(a).

.. a-=1.

..>>

>

>>

> a=

3>>

>

while a>2:

...print

(a).

.. a-=1.

..

'''

'''>>

> a.extend(2,

3)traceback (most recent call last)

: file ""

, line 1,in

typeerror: extend(

) takes exactly one argument (

2 given)

>>

> a.extend(2)

traceback (most recent call last)

: file ""

, line 1,in

typeerror:

'int'

object

isnot iterable

>>

>>

> a[1

,2,2

,4,[

2,4]

]

extend()不是

>>

> a=[1

,2]>>

> b=[2

,4]>>

> a.extend(b)

>>

> a[1

,2,2

,4]>>

>>

> a[1

,2,2

,4,[

2,4]

]

方法名相同的情況下,例如方法名內部有重名的方法和引數,呼叫的情況

def

name1

(collection)

:print

('外面的name1,引數:collection:'

,collection)

defname1

(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

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

0123

4#其次

>>

> s=[1

,2,3

,4,5

]>>

> length=

len(s)

>>

>

for i in

range

(length)

:#所以,這裡完全不用-1,類似於,因為range()會減去1,這就抵消掉了陣列長度比陣列下標多了1這個屬性說造成的訪問陣列會超出index這個trouble。..

.print

(s[i]).

..12

345>>

> length

5

range反向迴圈、反向遞減、將步長設定為負數就好了,注意要調換開始和結束的位置

'''

'''>>

>

for i in

range(5

,3,-

1):#從5開始,到3結束..

.print

(i)...

54>>

>

這樣range(0,0)並不會丟擲異常,而是什麼也不輸出

>>

>

for i in

range(0

,0):

...print

(i)...

>>

>

Python 語言中經常有疑惑的地方

可以這樣,不用儲存遞迴中的變數 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 d...

Python 語言中經常有疑惑的地方

可以這樣,不用儲存遞迴中的變數 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 d...

Java中經常遇到的類執行順序

下面是我寫的乙個demo package com.bw author brickworker 關於類color的描述 測試單個類的執行順序問題 public classcolor 靜態 塊 static 非靜態 塊 一般方法 void run public staticvoid main strin...