Pythonでfor文のループ処理を行う方法
みなさまおはこんばんにちは、せなです
今回はPythonのforループの処理について解説していきたいと思います
一般的なforのループ処理
以下のようにしてリストから値を取得するのが一般的なfor文の使い方かと思います
fruit_list = ['grape', 'apple', 'orange']
for fruit in fruit_list:
    print(fruit)
----結果----
grape
apple
orangeforのループを途中で終了:break
以下のように「break」をforの処理途中に記述すると「break」が行われた時点でforのループから抜けます
fruit_list = ['grape', 'apple', 'orange']
for fruit in fruit_list:
    if fruit == 'apple':
       print('BREAK')
       break
    print(fruit)
----結果----
grape
BREAK以下のようにforの外に記述している処理は実行されます
fruit_list = ['grape', 'apple', 'orange']
for fruit in fruit_list:
    if fruit == 'apple':
        print('BREAK')
        break
    print(fruit)
print('banana')
----結果----
grape
BREAK
bananaforの特定処理をスキップ:continue
以下のように「continue」をforの処理途中に記述すると「continue」が行われた時点でforのループの次に進みます 
fruit_list = ['grape', 'apple', 'orange']
for fruit in fruit_list:
    if fruit == 'apple':
        print('SKIP')
        continue
    print(fruit)
----結果----
grape
SKIP
orangeforのループ処理後の処理:else
以下のようにループの終了後に「else」を記述すると必ずその処理を行うことができます
fruit_list = ['grape', 'apple', 'orange']
for fruit in fruit_list:
    print(fruit)
else:
    print(banana)
----結果----
grape
apple
orange
banana以下のように「break」でループを抜けた場合には「else」の処理は実行されません
fruit_list = ['grape', 'apple', 'orange']
for fruit in fruit_list:
    if fruit == 'apple':
        print('BREAK')
        break
    print(fruit)
else:
    print(banana)
----結果----
grape
BREAKリストの逆順でforループ処理:reversed()関数
「reversed()」関数を使用するとリストの逆順から処理を行うことができます
fruit_list = ['grape', 'apple', 'orange']
for fruit in reversed(fruit_list):
    print(fruit)
----結果----
orange
apple
grapeforループ中のindex(カウンタ)処理
一般的なindex処理
forの処理中にindexをカウントしながら処理をしたいときには以下のように記述します
fruit_list = ['grape', 'apple', 'orange']
index = 0
for fruit in fruit_list:
    print('インデックス:' + str(index) + ' 値:' + fruit)
    index += 1
----結果----
インデックス:0 値:grape
インデックス:1 値:apple
インデックス:2 値:orange簡潔なindex処理:range()関数
以下のように「range()」関数を使用することでindexの記述を簡潔にすることができます
fruit_list = ['grape', 'apple', 'orange']
for index in range(len(fruit_list)):
    print('インデックス:' + str(index) + ' 値:' + fruit_list[index])
----結果----
インデックス:0 値:grape
インデックス:1 値:apple
インデックス:2 値:orange要素とindexの同時処理:enumerate()関数
以下のように「enumerate()」関数を使用することでindexと要素の両方を同時に取得することもできます
fruit_list = ['grape', 'apple', 'orange']
for index, fruit in enumerate(fruit_list):
    print('インデックス:' + str(index) + ' 値:' + fruit)
----結果----
インデックス:0 値:grape
インデックス:1 値:apple
インデックス:2 値:orange複数のリストの同時処理:zip()関数
「zip()」関数を使用することで複数のリストを同時にループさせることができます
fruit_list = ['grape', 'apple', 'orange']
vegetable_list = ['cabbage', 'carrots', 'tomato']
meat_list = ['beef', 'pork', 'chicken']
for fruit, vegetable, meat in zip(fruit_list, vegetable_list, meat_list):
    print('果物:' + fruit + ' 野菜:' + vegetable + ' お肉:' + meat)
----結果----
果物:grape 野菜:cabbage お肉:beef
果物:apple 野菜:carrots お肉:pork
果物:orange 野菜:tomato お肉:chicken「zip()」関数だと要素数の異なるリスト同士の際に最も要素数の少ない要素までしか処理が行われません
fruit_list = ['grape', 'apple']    #要素が2つしかない
vegetable_list = ['cabbage', 'carrots', 'tomato']
meat_list = ['beef', 'pork', 'chicken']
for fruit, vegetable, meat in zip(fruit_list, vegetable_list, meat_list):
    print('果物:' + fruit + ' 野菜:' + vegetable + ' お肉:' + meat)
----結果----
果物:grape 野菜:cabbage お肉:beef
果物:apple 野菜:carrots お肉:pork要素数を考慮せずに処理を行いたい場合は「itertools.zip_longest()」関数を使用してください
import itertools
fruit_list = ['grape', 'apple']    #要素が2つしかない
vegetable_list = ['cabbage', 'carrots', 'tomato']
meat_list = ['beef', 'pork', 'chicken']
for fruit, vegetable, meat in itertools.zip_longest(fruit_list, vegetable_list, meat_list):
    print('果物:' + str(fruit) + ' 野菜:' + vegetable + ' お肉:' + meat)
----結果----
果物:grape 野菜:cabbage お肉:beef
果物:apple 野菜:carrots お肉:pork
果物:None 野菜:tomato お肉:chicken上のように要素のないものには変わりにNoneが使用されるようになります
リンク:Pythonドキュメント:zip()
リンク:Pythonドキュメント:itertools.zip_longest()
dict型のforループ処理
dict型のオブジェクトをforループで処理するときにキーを取得するときには「keys()」関数を使用します
dict = {'Key1': 1, 'Key2': 2, 'Key3': 3}
for k in dict.keys():
    print(k)
----結果----
Key1
Key2
Key3値を取得する時には「values()」関数を使用します
dict = {'Key1': 1, 'Key2': 2, 'Key3': 3}
for v in dict.values():
    print(v)
----結果----
1
2
3キーと値の両方を取得するときには「items()」関数を使用します
dict = {'Key1': 1, 'Key2': 2, 'Key3': 3}
for k, v in dict.items():
    print(k, v)
----結果----
Key1 1
Key2 2
Key3 3
ディスカッション
コメント一覧
まだ、コメントがありません