Python中流程控制-for迴圈(序列/字典舉例)
阿新 • • 發佈:2018-11-27
流程控制-for迴圈(序列)
迴圈
迴圈是一個結構,導致程式要重複一定的次數。
條件下迴圈也是如此,當然條件變為假,迴圈結束。
for迴圈:
在序列裡,使用for迴圈遍歷。
語法:
for iterating_var in sqquence:
statement(s)
舉例:
(例1)for用法舉例
In [1]: a = 'ABC' In [2]: a Out[2]: 'ABC' In [3]: for i in a: ...: print i ...: A B C In [4]: for i in a: print i, #加逗號顯示在同一行用空格隔開。 ...: A B C
(例2)list的for迴圈
In [5]: list1 = [1,3,4,5]
In [6]: list1
Out[6]: [1, 3, 4, 5]
In [7]: for i in list1:
...: print i
...:
1
3
4
5
(例3)range()函式用法:
In [11]: range(1,11) Out[11]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] In [12]: range(11) Out[12]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] In [13]: range(1,11,2) Out[13]: [1, 3, 5, 7, 9] #指令碼:打印出1到10的10個數字: #!/usr/bin/python for i in range(1,11): print i 執行結果: [root[@localhost](https://my.oschina.net/u/570656) python]# python for.py 1 2 3 4 5 6 7 8 9 10 [root[@localhost](https://my.oschina.net/u/570656) python]#
(例4)print加","顯示為一行
指令碼1:
#!/usr/bin/python
print [i for i in range(1,11)]
執行結果:
[root[@localhost](https://my.oschina.net/u/570656) python]# python for.py
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[root[@localhost](https://my.oschina.net/u/570656) python]#
指令碼2:
#!/usr/bin/python
print [i*2 for i in range(1,11)]
執行結果:
[root[@localhost](https://my.oschina.net/u/570656) python]# python for.py
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
[ [email protected] python]#
指令碼3 求1,10內的偶數:
#!/usr/bin/python
print [i for i in range(1,11) if i%2==0]
執行結果:
[[email protected]localhost python]# python for.py
[2, 4, 6, 8, 10]
[[email protected] python]#
(例5)列表重寫:
指令碼4 求1,10內的偶數的乘方:
#!/usr/bin/python
for i in [i**2 for i in range(1,11) if i%2==0]:
print i,
執行結果:
[[email protected] python]# python for.py
4 16 36 64 100
[[email protected] python]#
指令碼5 求1到100所有數加到一起的和:
#!/usr/bin/python
sum = 0
for i in range(1,101):
sum = sum + i
print sum
執行結果:
[[email protected] python]# python for1.py
5050
(例6)xrange()
產生的是物件,節省記憶體:
xrange(10)
In [3]: a = xrange(20)
In [4]: type(a)
Out[4]: xrange
In [6]: for i in a:
print i,
...:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
流程控制-for迴圈(字典)
生成字典:
In [14]: dic = dic.fromkeys('abcdef','100')
In [15]: type(dic)
Out[15]: dict
In [16]: dic
Out[16]: {'a': '100', 'b': '100', 'c': '100', 'd': '100', 'e': '100', 'f': '100'}
(例1)打印出鍵值:
In [18]: for k in dic:
....: print k
....:
a
c
b
e
d
f
(例2)打印出鍵值和value
In [19]: for k in dic:
....: print k,dic[k]
....:
a 100
c 100
b 100
e 100
d 100
f 100
格式化輸出
In [21]: for k in dic:
print "%s --->> %s " % (k,dic[k])
....:
a --->> 100
c --->> 100
b --->> 100
e --->> 100
d --->> 100
f --->> 100
(例3)items()方法:
dic.items():返回的是列表 :
In [22]: dic.items()
Out[22]:
[('a', '100'),
('c', '100'),
('b', '100'),
('e', '100'),
('d', '100'),
('f', '100')]
In [23]: for i in dic.items():print i
('a', '100')
('c', '100')
('b', '100')
('e', '100')
('d', '100')
('f', '100')
In [24]: for k,v in dic.items():print k,v
a 100
c 100
b 100
e 100
d 100
f 100
(例4)dic.iteritems() 方法:
dic.iteritems() 返回的是物件:
In [25]: for k,v in dic.iteritems():print k,v
a 100
c 100
b 100
e 100
d 100
f 100
一個print 可以輸出一個換行符:
(例5)舉例乘法口訣:
#!/usr/bin/python
for i in xrange(1,10):
for j in xrange(1,i+1):
print "%sx%s=%s" % (j,i,j*i),
print
執行結果:
[[email protected] python]# python koujun.py
1x1=1
1x2=2 2x2=4
1x3=3 2x3=6 3x3=9
1x4=4 2x4=8 3x4=12 4x4=16
1x5=5 2x5=10 3x5=15 4x5=20 5x5=25
1x6=6 2x6=12 3x6=18 4x6=24 5x6=30 6x6=36
1x7=7 2x7=14 3x7=21 4x7=28 5x7=35 6x7=42 7x7=49
1x8=8 2x8=16 3x8=24 4x8=32 5x8=40 6x8=48 7x8=56 8x8=64
1x9=9 2x9=18 3x9=27 4x9=36 5x9=45 6x9=54 7x9=63 8x9=72 9x9=81
[[email protected] python]#