トップ «前の日記(2012-05-20(Sun)) 最新 次の日記(2012-05-24(Thu))» 編集

屑俺日記

僕の備忘録(PC、UN*X、ネットワーク関連が中心)なんです。
自分の書いたところは適当(な時とか)に書き換えますので御了承を。


2012-05-21(Mon) 日食が終わってから晴れた

frozenset

タプル同様、書き換えできないデータ型。

>>> Set = {"foo", "bar", "baz"}
>>> FSet = frozenset(Set)
>>> FSet
frozenset({'baz', 'foo', 'bar'})
>>> Set == FSet
True
>>> Set is FSet
False
>>> dir(FSet)
['__and__', '__class__', '__contains__', '__delattr__', '__doc__', \
 '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', \
 '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', \
 '__ne__', '__new__', '__or__', '__rand__', '__reduce__', \
 '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__',\
 '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__',\
 '__xor__', 'copy', 'difference', 'intersection', 'isdisjoint',\
 'issubset', 'issuperset', 'symmetric_difference', 'union']
>>> FSet.pop()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'pop'
>>> Set.pop()
'bar'

リストの中にリスト

ドキュメントをぼんやり見ているうちに気づいた。
意味が分かるまでしばらくかかった。

>>> list = []
>>> list.append(["foo"])
>>> list
[['foo']]
>>> list.append(list[0])
>>> list
[['foo'], ['foo']]
>>> list[1].append("bar")
>>> list
[['foo', 'bar'], ['foo', 'bar']]
>>> list[0] is list[1]
True
>>> list[1] = list[0][:]
>>> list[0] is list[1]
False
>>> list
[['foo', 'bar'], ['foo', 'bar']]
>>> list[0].pop()
'bar'
>>> list
[['foo'], ['foo', 'bar']]

ついでにこんなの

>>> l1, l2, l3 = [["foo"]] * 3
>>> l1, l2, l3
(['foo'], ['foo'], ['foo'])
>>> l1 is l3
True
 
>>> l1, l2, l3 = [["bar"] for x in range(3)]
>>> l1, l2, l3
(['bar'], ['bar'], ['bar'])
>>> l1 is l3
False

range type

これも同じドキュメントに載っていた。

>>> foo = range(3)
>>> for x in foo: print(x)
... 
0
1
2

リンクはご自由にどうぞ。でもURLや内容が変った場合はあしからず。

index.htmlは ここから。