ページへ戻る

− Links

 印刷 

Python​/文字列の分割 のバックアップの現在との差分(No.2) :: NJF Wiki

xpwiki:Python/文字列の分割 のバックアップの現在との差分(No.2)

« Prev[4]  Next »[5]
2: 2016-12-05 (月) 13:30:02 njf[6] ソース[7] バックアップ No.2 を復元して編集[8] 現: 2017-09-05 (火) 14:40:09 njf[6] ソース[9] 編集[10]
Line 3: Line 3:
 testStr = "a,b,c,d,e,f"  testStr = "a,b,c,d,e,f"
 + 
 print testStr.split(",")  print testStr.split(",")
Line 13: Line 13:
 testStr = "a,b,c,d,e,f"  testStr = "a,b,c,d,e,f"
 + 
 print testStr.split(",",2)  print testStr.split(",",2)
Line 26: Line 26:
 testStr = "123456789"  testStr = "123456789"
 + 
 print list(testStr)  print list(testStr)
Line 32: Line 32:
 ['1', '2', '3', '4', '5', '6', '7', '8', '9']  ['1', '2', '3', '4', '5', '6', '7', '8', '9']
-ただし、Pythonの文字列はもともとリストのような振る舞いをするので、リストに変換してもさほど使い道はないかも知れません。+ただし、Pythonの文字列はもともとリストのような振る舞いをするので、参照するだけならリストに変換してもさほど使い道はないかも知れません。
例えば、文字列のままでループも可能です。 例えば、文字列のままでループも可能です。
 testStr = "123456789"  testStr = "123456789"
 + 
 for c in testStr:  for c in testStr:
     print c      print c
Line 51: Line 51:
 8  8
 9  9
 +
 +しかし、頻繁に大きな文字列を編集するならリストの方が高速なので、その場合に文字列のリストへの変換が役立つことがあります。
« Prev[4]  Next »[5]