Urchin_learningのPython勉強ブログ

ふと思い立ってPythonを勉強しようと思い立ちました。その勉強日記。

Python 勉強2日目 配列 tuple / list / dict / set

Python 勉強2日目 配列

1.tuple

上が書いたコード

下が返された値

import datetime

def get_today():

    today = datetime.datetime.today()
    value = (today.year,today.month,today.day)

    return value

test_tuple = get_today()

print(test_tuple)
print(test_tuple[0])
print(test_tuple[1])
print(test_tuple[2])

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(2021, 5, 8)
2021
5
8

後から他の要素を足したり、要素を消したりは出来ない。

 

2.list

上が書いたコード

下が返された値

#tupleは()で囲う、listはで囲う
test_list = ["urchin","-","izm",".","com"]
print(test_list)

print("--------------------------")

for i in test_list:
    print(i)

test_list2 = 
print(test_list2)

print("-----------------")

#appendは末尾への追加
test_list2.append("Urchin")
test_list2.append("-")
test_list2.append("izm")
test_list2.append("-")
test_list2.append("com")

#insertは入れる位置を指定できる
print(test_list2)
print("------------------------------")
test_list2.insert(0,"http://WWW.")
test_list2.insert(7,"/details")

print(test_list2)

print("------------------------------")

#popは指定した位置の要素を削除し、削除した要素を返す
#指定しないと末尾を消す。
print(test_list2)
print(test_list2.pop(0))
print(test_list2.pop())
print(test_list2)

#indexはある要素がlist内の最初に見つかった位置を返してくれる
print(test_list2.index("-"))

#countは指定の要素がlist内でいくつあるかを数えて返してくれる
test_list3 = ["1","0","3","1","1","2"]
print(test_list3.count("1"))

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

['urchin', '-', 'izm', '.', 'com']
--------------------------
urchin
-
izm
.
com
[]
-----------------
['Urchin', '-', 'izm', '-', 'com']
------------------------------
['http://WWW.', 'Urchin', '-', 'izm', '-', 'com', '/details']
------------------------------
['http://WWW.', 'Urchin', '-', 'izm', '-', 'com', '/details']
http://WWW.
/details
['Urchin', '-', 'izm', '-', 'com']
1
3

 

3.dict (ディクショナリ)

上が書いたコード

下が返された値

#dictは{}で包んで"key":"value"の形でセット化して書いていく
test_dict1 = {"青":"blue","赤":"red","黄":"yellow","緑":"green"}
print(test_dict1)

print("----------------------------------")

for i in test_dict1:
    print(i)
    print(test_dict1[i])
    print("------------------------")

#getを使えばkeyとセットのvalueを返してくれる
#getはそのkeyが無くてもエラーにならない

print("----------------------------------")
print(test_dict1)

print(test_dict1.get("赤"))
print(test_dict1.get("蒼"))
print(test_dict1.get("翠","this video has been deleted" ))
print("----------------------------------")
#dict_obj["key"] = ""で要素を追加できる
test_dict1["紫"] = "purple"
test_dict1["白"] = "white"
test_dict1["黒"] = "black"
print(test_dict1)
print("----------------------------------")
#delで要素を削除できる
del test_dict1["紫"]
print(test_dict1)
print("----------------------------------")
#keys や valuesはkey or valueのみをlist化して返してくれる
print(test_dict1.keys())
print(test_dict1.values())

#itemsはkeyとvalueを同時に取得してくれる
print("----------------------------------")

for keyvalue in test_dict1.items():
    print(keyvalue)

#inで指定のkeyを保持しているかT/Fで判定してくれる

print("赤" in test_dict1)
print("茶" in test_dict1)
print("blue" in test_dict1#valueで保持しててもfalse

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

{'青': 'blue', '赤': 'red', '黄': 'yellow', '緑': 'green'}
----------------------------------

blue
------------------------

red
------------------------

yellow
------------------------

green
------------------------
----------------------------------
{'青': 'blue', '赤': 'red', '黄': 'yellow', '緑': 'green'}
red
None
this video has been deleted
----------------------------------
{'青': 'blue', '赤': 'red', '黄': 'yellow', '緑': 'green', '紫': 'purple', '白': 'white', '黒': 'black'}
----------------------------------
{'青': 'blue', '赤': 'red', '黄': 'yellow', '緑': 'green', '白': 'white', '黒': 'black'}----------------------------------
dict_keys(['青', '赤', '黄', '緑', '白', '黒'])
dict_values(['blue', 'red', 'yellow', 'green', 'white', 'black'])
----------------------------------
青 blue
赤 red
黄 yellow
緑 green
白 white
黒 black
True
False
False

 

4.set

上が書いたコード

下が返された値

#setもdict同様{}で囲うよ
#setは重複した要素は持てないよ
test_set ={"red","blue","yellow","red"}
print(test_set)
for i in test_set:
    print(i)

print("-------------------------")

#空のsetを作るには test_obj = set()というようにする
#dictは dict_obj = {}でよかったのに
test_set2 = set()

#単一要素の追加はadd 、複数はupdate
test_set2.add("red")
print(test_set2)
test_set2.update({"blue","yellow","green","balck","white"})
print(test_set2#追加の順番はランダムみたい

#削除はremove,discard removeは指定した要素がないとエラーになる
test_set2.remove("red")
print(test_set2)
test_set2.discard("blue")
print(test_set2)

#frozensetはadd,update,remove,discardなどの追加/削除を使うとエラー

test_set3 = frozenset({"赤","青","黄"})
print(test_set3)
test_set3.add("黒")

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

{'red', 'blue', 'yellow'}
red
blue
yellow
-------------------------
{'red'}
{'red', 'blue', 'green', 'white', 'balck', 'yellow'}
{'blue', 'green', 'white', 'balck', 'yellow'}
{'green', 'white', 'balck', 'yellow'}
frozenset({'赤', '黄', '青'})
Traceback (most recent call last):
File "c:\Users\nhiro\Desktop\Python-Practice\Urchin07.py", line 30, in <module>
test_set3.add("黒")
AttributeError: 'frozenset' object has no attribute 'add'

 

色々試しながら書いてたけど、listではできてdictではできないとかがあってややこしい。。

 

お次はスライス