ばーろぐわにる

SIerからWEB系?インフラエンジニアにジョブチェンジした見習いの備忘録。投稿内容は私個人の意見であり、所属企業・部門見解を代表するものではありません。

【PART2】Python勉強メモ

タプル

>>> my_tuple = ("hoge", "fuga")
>>> print(my_tuple)
('hoge', 'fuga')

代入するオブジェクトが1つの場合はhoge = ([object],)のように最後に,をつけること。 これがないと算術演算子()と認識されてしまう。

>>> my_tuple.append("foo")
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'tuple' object has no attribute 'append'
>>> my_tuple.pop()
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'tuple' object has no attribute 'pop'
>>> my_tuple[0] = "fuga"
Traceback (most recent call last):
File "", line 1, in
TypeError: 'tuple' object does not support item assignment

タプルはイミュータブル(変更不可能)のため、pop,appendも使えず。ていうかメソッドが紐づいてない。 要素の変更ももちろんできない。

>>> print(my_tuple[0])
hoge
>>>

参照は配列と同じ方法。

set型について

ここを参考にした。 一言で言うと、"重複した要素がなく、要素の順番がないリスト"とのこと。もはやリストではない?だから集合。

>>> myset1 = set([0,1,2])
>>> myset2 = set([0,1,2,2,2,2,2,3,3,4])
>>> print(myset1)
{0, 1, 2}
>>> print(myset2)
{0, 1, 2, 3, 4}

集合ということで集合計算もできる。

>>> set_a = set([0,1,2,3])
>>> set_b = set([2,3,4,5])
>>>
>>>
>>> print (set_a | set_b)
{0, 1, 2, 3, 4, 5}
>>> print (set_a & set_b)
{2, 3}
>>> print (set_a - set_b)
{0, 1}
>>> print (set_a ^ set_b)
{0, 1, 4, 5}

かんそう

set型はどういうときに使うのだろう。機械学習系? 今日も業務が長すぎてあんまり勉強できず。休み時間と通勤時間程度。 だらだら残業が多くなってきてる気がする。よくないです。