Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

@ymyk

【随時更新(予定)】Pythonのデータを格納するオブジェクト

はじめに

自分の備忘もかねて、基本中の基本をまとめていきます

データを格納するオブジェクト

リスト(list)
タプル(tuple)
集合(set)
辞書(dictionary)

タプル(Tuple)

tuple.py
location = (13.4125, 103.866667)

集合(Set)

set.py
numbers = {1, 2, 6, 3, 1, 1, 6}

リスト(List)

list.py
list_of_random_things = [1, 3.4, 'a string', True] 
#listはstring, int, floatすべて可能

list_of_month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec"]
                   0      1      2      3      4      5       ......
#listのindexは0から始まる(一番初めの要素からの距離)
list_of_month[1] #["Feb"]

#list[(含む):(含まない)]
list_of_month[1:4] #["Feb", "Mar", "Apr" ]
list_of_month[:3] #["Jan", "Feb", "Mar" ]
list_of_month[5:] #["Jun", "July", "Aug", "Sep", "Oct", "Nov", "Dec"] 

#上書き可能(可変)
my_lst = [1, 2, 3, 4, 5]
my_lst[0] = 'one'
print(my_lst) #['one', 2, 3, 4, 5]

辞書(Dictionary)

dictionary.py
elements = {"hydrogen": 1, "helium": 2, "carbon": 6}
animals = {'dogs': [20, 10, 15, 15], 'cats': [3,4,2,8,2,4], 'rabbits': [2, 3, 3]}
#検索1
print(animals['dogs']) #[20, 10, 15, 15]
#検索2
print(animals['dogs'][3]) #15
#新しいkeyとvalueの追加
animals["birds"] = [4,5,5,6]  
print(animals) #{'dogs': [20, 10, 15, 15], 'cats': [3,4,2,8,2,4], 'rabbits': [2, 3, 3], 'birds': [4,5,5,6]}

まとめ

データ型 順序 変更
追加
削除
表記
タプル(Tuple) × () or tuple() (5.7, 4, 'yes', 5.7)
セット(Set) × { } or set() {5.7, 4, 'yes'}
リスト(List) [] or list() [5.7, 4, 'yes', 5.7]
辞書(Dictionary) × { } or dict() {'Jun': 75, 'Jul': 89}

参考

はじめてにQiitaに当たって書き方を参考に
Qiita書き方

これを使って練習したい(願望)
Hackerrank

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
2
Help us understand the problem. What are the problem?