Hatena::Grouppython

zorioの日記

 | 

2010-03-22

文字列処理

13:43

同じ内容のリテラルでも、2回書いたら別のインスタンスになった

# -*- encoding: utf-8 -*-

str1 = "String is immutable"
str2 = "String is immutable"
print str1 == str2 # => True
print str1 is str2 # => False
print id(str1), id(str2) # => 43, 44

文字列を分割して配列に保存

# -*- encoding: utf-8 -*-

delim = ","
source = "abc, def, ghi, ,"

# string.splitを使う
print source.split(delim) # => ['abc', ' def', ' ghi', ' ', '']

# 正規表現で自前で分割してみる
import re
reg = re.compile(delim)
list = []
end = 0
for m in reg.finditer(source):
  list.append(source[end:m.start()])
  end = m.end()
list.append(source[end:])

print list # => ['abc', ' def', ' ghi', ' ', '']

print list == source.split(delim) # => True

axkvduthzpaxkvduthzp 2011/03/15 20:57 vzOLlq <a href="http://gsxiwyekxxxi.com/">gsxiwyekxxxi</a>, [url=http://tvemvemltviq.com/]tvemvemltviq[/url], [link=http://riebovoddrkr.com/]riebovoddrkr[/link], http://mwkcikamwzat.com/

FrancineFrancine 2012/12/07 16:08 A good many valubales you've given me.

nuwelgbsjnuwelgbsj 2012/12/08 16:50 c22DyI <a href="http://mbscsbzmsgwf.com/">mbscsbzmsgwf</a>

bxkplcwbxkplcw 2012/12/09 22:11 6XRJKT , [url=http://himmxvrjvlqo.com/]himmxvrjvlqo[/url], [link=http://wbaxnjnxzpal.com/]wbaxnjnxzpal[/link], http://fpfzrwskrjms.com/

uofixknmtieuofixknmtie 2012/12/11 13:37 3QT4iY <a href="http://fvbekztndcrj.com/">fvbekztndcrj</a>

ゲスト



トラックバック - http://python.g.hatena.ne.jp/zorio/20100322
 |