LoginSignup
1
0

【Python】dict型/list型でgetとset

Last updated at Posted at 2023-09-07

dict型をget

getを使用すると存在しないキー「グループ名」を指定した場合にエラーが出ない

student_dict = {'所属ID': 102, '学年ID': '01', 'クラスID': 4, '名前': '星宮いちご'}
school_year = student_dict.get("学年ID")
print(school_year)
# 01

存在しないキーをカラムで指定するとエラーが出る

student_dict = {'所属ID': 102, '学年ID': '01', 'クラスID': 4, '名前': '星宮いちご'}
school_year = student_dict['グループID']
print(school_year)
# KeyError: 'グループID'

dict型をset

student_dict = {'所属ID': 102, '学年ID': '01', 'クラスID': 4, '名前': '星宮いちご'}
school_year = set([student_dict.get("学年ID")])
print(school_year)
# {'01'}

setの中身が文字列のとき[]で囲む
囲まないときは、文字列が分解される

student_dict = {'所属ID': 102, '学年ID': '01', 'クラスID': 4, '名前': '星宮いちご'}
year_list = set(student_dict.get("学年ID"))
print(year_list)
# {'0', '1'}

list型をget

student_list = [
    {'所属ID': 100, '学年ID': '01', 'クラスID': 1, '名前': '星宮いちご'},
    {'所属ID': 100, '学年ID': '01', 'クラスID': 2, '名前': '霧矢あおい'},
    {'所属ID': 100, '学年ID': '01', 'クラスID': 4, '名前': '紫吹蘭'},
    {'所属ID': 100, '学年ID': '01', 'クラスID': 4, '名前': '栗栖川おとめ'},
    {'所属ID': 100, '学年ID': '02', 'クラスID': 1, '名前': '藤堂ユリカ'},
    {'所属ID': 100, '学年ID': '02', 'クラスID': 3, '名前': '北大路さくら'},
    {'所属ID': 101, '学年ID': '04', 'クラスID': 1, '名前': '一ノ瀬かえで'},
]
for l in student_list:
    year = l.get("学年ID")
    print(year,end=" ")
# 01 01 01 01 02 02 04

list型をset

student_list = [
    {'所属ID': 100, '学年ID': '01', 'クラスID': 1, '名前': '星宮いちご'},
    {'所属ID': 100, '学年ID': '01', 'クラスID': 2, '名前': '霧矢あおい'},
    {'所属ID': 100, '学年ID': '01', 'クラスID': 4, '名前': '紫吹蘭'},
    {'所属ID': 100, '学年ID': '01', 'クラスID': 4, '名前': '栗栖川おとめ'},
    {'所属ID': 100, '学年ID': '02', 'クラスID': 1, '名前': '藤堂ユリカ'},
    {'所属ID': 100, '学年ID': '02', 'クラスID': 3, '名前': '北大路さくら'},
    {'所属ID': 101, '学年ID': '04', 'クラスID': 1, '名前': '一ノ瀬かえで'},
]
year = set(l.get("学年ID") for l in student_list)
print(year)
# {'04', '01', '02'}

for内部表記を使用してdict扱いにする

応用

student_list = [
    {'所属ID': 100, '学年ID': '01', 'クラスID': 1, '名前': '星宮いちご'},
    {'所属ID': 100, '学年ID': '01', 'クラスID': 2, '名前': '霧矢あおい'},
    {'所属ID': 100, '学年ID': '01', 'クラスID': 4, '名前': '紫吹蘭'},
    {'所属ID': 100, '学年ID': '01', 'クラスID': 4, '名前': '栗栖川おとめ'},
    {'所属ID': 100, '学年ID': '02', 'クラスID': 1, '名前': '藤堂ユリカ'},
    {'所属ID': 100, '学年ID': '02', 'クラスID': 3, '名前': '北大路さくら'},
    {'所属ID': 101, '学年ID': '04', 'クラスID': 1, '名前': '一ノ瀬かえで'},
]
# 所属IDのlistを作成
affiliation_list = set(l.get("所属ID") for l in student_list)
for affi in affiliation_list:
    # 所属IDのlistと所属IDの値が同値のlistを取得
    affi_same = list(filter(lambda x : affi == x["所属ID"],student_list))
    print(affi_same,end="\n")
# [{'所属ID': 100, '学年ID': '01', 'クラスID': 1, '名前': '星宮いちご'}, {'所属ID': 100, '学年ID': '01', 'クラスID': 2, '名前': '霧矢あおい'}, {'所属ID': 100, '学年ID': '01', 'クラスID': 4, '名前': '紫吹蘭'}, {'所属ID': 100, '学年ID': '01', 'クラスID': 4, '名前': '栗栖川おとめ'}, {'所属ID': 100, '学年ID': '02', 'クラスID': 1, '名前': '藤堂ユリカ'}, {'所属ID': 100, '学年ID': '02','クラスID': 3, '名前': '北大路さくら'}]
# [{'所属ID': 101, '学年ID': '04', 'クラスID': 1, '名前': '一ノ瀬かえで'}]
1
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0