こもろぐ @tenkoma

What We Find Changes Who We Become -- Peter Morville著『アンビエント・ファインダビリティ 』

広告:本ブログで紹介している書籍等商品の紹介でAmazonアソシエイトを利用していることがあります。

皆Py書房 -- That's a side of The Python I didn't know(1)

p.28 set型

そういえばset型って使ったことがないや.試してみるべきだな.(p.128でも触れるらし)

>>> se = set((1,2,3,4,5,6))
>>> se
set([1, 2, 3, 4, 5, 6])
>>> se.pop()
1
>>> se
set([2, 3, 4, 5, 6])

インデクシングできないところなどリストに似てリストに非ずのようだ.

同 整数同士の割り算は切り捨てではない

>>> -1/2
-1

p.33 文字列型

文字列型とユニコード文字列型の説明があるけど,かな漢字を含む文字列は極力ユニコード文字列を使うようにした方がいいのかな?

p.44 ユニコード文字列を使う

よくわかってないんだけど,とりあえず端末の出力を載せておこう.

cmd.exe
>>> ustr = u"日本語"
>>> ustr
u'\u65e5\u672c\u8a9e'
>>> print ustr
日本語
>>>
IDLE 1.1.3
>>> ustr = u"日本語"
>>> print ustr
“ú–{Œê
>>> print ustr.encode('utf-8')
ツ禿コツ本ツ古ェ
>>> 
>>> print ustr.encode('mbcs')
?u?{?e
>>> print ustr.encode('shift-jis')

Traceback (most recent call last):
  File "<pyshell#5>", line 1, in -toplevel-
    print ustr.encode('shift-jis')
UnicodeError: Shift_JIS encoding error: invalid character \x93
>>> print ustr.encode('iso-8859-1')
日本語
IPython 0.7.2 (空白行を端折ってます.)
In [1]: ustr = u"日本語"
In [2]: print ustr
---------------------------------------------------------------------------
exceptions.UnicodeError                              Traceback (most recent call last)
C:\Python24\<ipython console>
UnicodeError: MS932 encoding error: invalid character \x93
In [3]: print ustr.encode('mbcs')
?u?{?e
In [4]: print ustr.encode('utf-8')
ツ禿コツ本ツ古ェ
In [5]: print ustr.encode('shift-jis')
---------------------------------------------------------------------------
exceptions.UnicodeError                              Traceback (most recent call last)
C:\Python24\<ipython console>
UnicodeError: Shift_JIS encoding error: invalid character \x93
In [6]: print ustr.encode('iso-8859-1')
日本語