Pythonのループの書き方いろいろ
更新日:2018.09.02
作成日:2011.11.21
Pythonでのループで書き方ってどうするんだろうって思って、ちょっと調べてみました。
通常のループ
for x in range(0, 10):
print x
辞書のループ
keys = [u"名前", u"ヨミガナ", u"Emailアドレス", u"住所"]
values = [u"meganii", u"メガニイ", u"email@address", u"住所"]
address = dict()
for key, value in zip(keys, values):
address[key] = value
for key, value in address.iteritems():
print key, value
dict(…)を使うと、キーを’‘で囲わなくても良いみたい
ループ内でインデックス値を使いたい場合
for i, x in enumerate(range(0,10)):
print i
print x
zipとenumerateを同時に使う
for i, (key,value) in enumerate(zip(keys, values)):
print i
print key
print value
参考
Related contents

TECH
2011.11.15
Pythonでスクレイピングに挑戦。〜mixiの日記をEvernoteへ放り込みたい〜

TECH
2011.11.14
いまさらだけど、Bot作る〜お天気取得〜

TECH
2021.03.27
【Tableau】Personal Access TokensでTableau REST APIを利用する

TECH
2017.06.18
EDINETとXBRLとは何か?Pythonで財務諸表のデータを取得する

TECH
2016.09.06
Hugoで人気記事を表示するためJSONを返すAPIサーバを作りData-driven Contentを試してみた

TECH
2016.08.13
PythonでTF-IDFによる文書推薦

TECH
2016.08.12
文書推薦

TECH
2016.08.11
Hugoで関連記事を表示する方法