0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

④for/if/forの考え方を日常生活のイベントに当てはめてみた!

0
Posted at

🔍Introduction

foriffor は “大きいまとまりを順番に見る → 条件に合うものだけ選ぶ → その中身をさらに全部処理する” という構造。
日常生活にもめちゃくちゃ自然に存在してる。

🏠 ① 町内会のゴミ当番(ブロック → 当番なら → ゴミ袋)

日常の流れ
  • ブロックを順番に確認する(for
  • 今日ゴミ当番のブロックだけ処理する(if
  • そのブロックのゴミ袋を全部チェックする(for
garbage_duty.py
town = {
    "Aブロック": {
        "is_gomi_day": True,
        "bags": ["燃えるゴミ", "プラゴミ", "紙ゴミ"]
    },
    "Bブロック": {
        "is_gomi_day": False,
        "bags": []
    },
    "Cブロック": {
        "is_gomi_day": True,
        "bags": ["燃えるゴミ", "", ""]
    }
}

for block_name, block_info in town.items():       # for(ブロック)
    print(f"--- {block_name} を確認中 ---")

    if block_info["is_gomi_day"]:                 # if(当番なら)
        print("今日はゴミ当番です。袋をチェックします。")

        for bag in block_info["bags"]:            # for(袋)
            print(f"{bag} を回収しました")

🍱 ② お弁当作り(おかずの種類 → 汁物以外 → 個別アイテム)

日常の流れ
  • おかずの種類を順番に見る(for
  • 汁物じゃない種類だけ詰める(if
  • その種類の個別のおかずを全部詰める(for
lunch_box.py
bento = {
    "main": ["唐揚げ", "ハンバーグ"],
    "soup": ["味噌汁"],
    "side": ["卵焼き", "ほうれん草", "ポテサラ"]
}

for category, items in bento.items():            # for(種類)
    if category != "soup":                       # if(汁物以外)
        print(f"--- {category} を詰める ---")
        for item in items:                       # for(個別アイテム)
            print(f"詰める: {item}")

🛒 ③ スーパーの棚(棚 → 安い商品だけ → 商品を全部チェック)

日常の流れ
  • 棚を順番に見る(for
  • 安い棚だけチェックする(if
  • その棚の商品を全部見る(for
supermarket_shelf.py
supermarket = {
    "お菓子": {"cheap": True, "items": ["ポッキー", "チョコ", "グミ"]},
    "飲み物": {"cheap": False, "items": ["", "コーラ", "お茶"]},
    "野菜": {"cheap": True, "items": ["にんじん", "玉ねぎ"]}
}

for shelf, info in supermarket.items():          # for(棚)
    if info["cheap"]:                            # if(安い棚)
        print(f"=== {shelf} コーナー(安い) ===")
        for item in info["items"]:               # for(商品)
            print(f"商品: {item}")

🚉 ④ 電車の運行管理(路線 → 遅延してる路線 → 駅を全部チェック)

日常の流れ
  • 路線を順番に見る(for
  • 遅延している路線だけ処理する(if
  • その路線の駅を全部確認する(for
train_operation.py
lines = {
    "東西線": {"delayed": True, "stations": ["中野", "高田馬場", "早稲田"]},
    "山手線": {"delayed": False, "stations": ["新宿", "渋谷", "品川"]},
}

for line, info in lines.items():                 # for(路線)
    if info["delayed"]:                          # if(遅延)
        print(f"--- {line} 遅延中 ---")
        for station in info["stations"]:         # for(駅)
            print(f"駅の状況確認: {station}")

🎯 Conclusion

foriffor は “大きい箱 → 条件に合う箱 → 中の小さい箱全部” の処理。
日常生活に自然に存在していて、Python に落とすとめちゃ理解しやすい。

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?