dict = { 'key1': 123, 'key2': 456, 'key3': 789 }
for key, value in dict.items():
print(key, value)
key1 123
key2 456
key3 789
list = [ 123, 456, 789 ]
for index, value in enumerate(list):
print(index, value)
0 123
1 456
2 789
= [ 'aaa', 'bbb', 'ccc' ]
seq1 = [ 'xxx', 'yyy', 'zzz' ]
seq2
for t in zip(seq1, seq2):
print(t)
for a, x in zip(seq1, seq2):
print(a, x)
('aaa', 'xxx')
('bbb', 'yyy')
('ccc', 'zzz')
aaa xxx
bbb yyy
ccc zzz
= 0
start = 10
end = 2
step for k in range(start, end, step):
print(k)
0
2
4
6
8
= range(0, 5)
seq
for k in seq:
print(k)
print()
for k in reversed(seq):
print(k)
0
1
2
3
4
4
3
2
1
0
print
とprint()
では改行の有無が変わるようだ。= [ 3, 1, 4, 1, 5 ]
seq
for k in seq:
print(k)
print()
for k in sorted(seq):
print(k)
print()
for k in sorted(set(seq)):
print(k)
3
1
4
1
5
1
1
3
4
5
1
3
4
5
= [ 3, 1, 4, 1, 5 ]
seq
if 4 in seq:
print('Exist')
else:
print('Not exist')
# => Exist
= [3, 1, 4, 1, 5, 9]
seq
for k in seq:
if 1 < k < 5:
print(k)
3
4
and
はショートカット演算子= [ 0, 1, 2, 3 ]
seq
for n in range(0,1000):
if 0 <= n < len(seq) and seq[n] > 0:
print(n)
1
2
3
= [ '', 0, True, False, 'Hello' ]
seq
for x in seq:
if x:
print(x)
True
Hello
http://docs.python.jp/3/tutorial/datastructures.html#looping-techniques