python字符串連接的三種方法及其效率、適用場景詳解


python字符串連接的方法,一般有以下三種:方法1:直接通過加號(+)操作符連接website=& 39;python& 39;+& 39;tab& 39;+& 39; com& 39;方法2

python字符串連接的方法,一般有以下三種:

方法1:直接通過加號(+)操作符連接

1
website  =  'python'  +  'tab'  +  '.com'

方法2:join方法

1
2
listStr  =  [ 'python' 'tab' '.com'
website  =  ''.join(listStr)

方法3:替換

1
website  =  '%s%s%s'  %  ( 'python' 'tab' '.com' )

 

下面再來說一下三種方法的不同

方法1,使用簡單直接,但是網上不少人說這種方法效率低

之所以說python 中使用 + 進行字符串連接的操作效率低下,是因為python中字符串是不可變的類型,使用 + 連接兩個字符串時會生成一個新的字符串,生成新的字符串就需要重新申請內存,當連續相加的字符串很多時(a+b+c+d+e+f+...) ,效率低下就是必然的了

 

方法2,使用略復雜,但對多個字符進行連接時效率高,只會有一次內存的申請。而且如果是對list的字符進行連接的時候,這種方法必須是首選

 

方法3:字符串格式化,這種方法非常常用,本人也推薦使用該方法

 

下面用實驗來說明字符串連接的效率問題。

1
2
3
比較對象:加號連接 VS  join 連接
python版本: python2.7
系統環境:CentOS

實驗一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# -*- coding: utf-8 -*-
from  time  import  time
def  method1():
     =  time()
     for  in  xrange ( 100000 ):
         =  'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab' + 'pythontab'
     print  time()  -  t
def  method2():
     =  time()
     for  in  xrange ( 100000 ):
         =  ' '.join([' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab ',' pythontab'])
     print  time()  - t
method1()
method2()

結果:

1
2
0.641695976257
0.341440916061

 

實驗二:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# -*- coding: utf-8 -*-
from  time  import  time
def  method1():
     =  time()
     for  in  xrange ( 100000 ):
         =  'pythontab' + 'pythontab' + 'pythontab' + 'pythontab'
     print  time()  -  t
def  method2():
     =  time()
     for  in  xrange ( 100000 ):
         =  ' '.join([' pythontab ',' pythontab ',' pythontab ',' pythontab'])
     print  time()  - t
method1()
method2()

結果:

1
2
0.0265691280365
0.0522091388702

 

上面兩個實驗出現了完全不同的結果,分析這兩個實驗唯一不同的是:字符串連接個數。

結論:加號連接效率低是在連續進行多個字符串連接的時候出現的,如果連接的個數較少,加號連接效率反而比join連接效率高


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM