# 跟老齐学python之关于循环的小伎俩

range

#! /usr/bin/env python
#coding:utf-8

aliquot = []

for n in range(1,100):
if n%3 == 0:
aliquot.append(n)

print aliquot

[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]

>>> aliquot = [ x for x in range(1,100) if x%3==0 ] #用list解析，本质上跟上面无太大差异
>>> aliquot
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]

>>> aliquot = range(3,100,3) #这种方法更简单。这是博客中一网友提供。
>>> aliquot
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]

>>> one = “ilikepython”
>>> new_list = [ one[i] for i in range(0,len(one),2) ]
>>> new_list
[‘i’, ‘i’, ‘e’, ‘y’, ‘h’, ‘n’]

>>> all_int = range(1,100)
>>> all_int
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>> aliquot = [ all_int[i] for i in range(len(all_int)) if all_int[i]%3==0 ]
>>> aliquot
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]

zip

zip是用于并行遍历的函数。

>>> list1 = range(2,10,2)
>>> list1
[2, 4, 6, 8]
>>> list2 = range(11,20,2)
>>> list2
[11, 13, 15, 17, 19]
>>> result = [ list1[i]+list2[i] for i in range(len(list1)) ]
>>> result
[13, 17, 21, 25]

zip完成上面的任务，是这么做的：

>>> list1
[2, 4, 6, 8]
>>> list2
[11, 13, 15, 17, 19]
>>> for a,b in zip(list1,list2):
… print a+b,

13 17 21 25

zip()的作用就是把list1和list2两个对象中的对应元素放到一个元组(a,b)中，然后对这两个元素进行操作。

>>> list1
[2, 4, 6, 8]
>>> list2
[11, 13, 15, 17, 19]
>>> zip(list1,list2)
[(2, 11), (4, 13), (6, 15), (8, 17)]

>>> result = zip(list1,list2)
>>> result
[(2, 11), (4, 13), (6, 15), (8, 17)]
>>> zip(*result)
[(2, 4, 6, 8), (11, 13, 15, 17)]

>>> infor = {}
>>> for k,v in myinfor.items():
… infor[v]=k

>>> infor
{‘python’: ‘lang’, ‘qiwsir.github.io’: ‘site’, ‘qiwsir’: ‘name’}

>>> dict(zip(myinfor.values(),myinfor.keys()))
{‘python’: ‘lang’, ‘qiwsir.github.io’: ‘site’, ‘qiwsir’: ‘name’}

>>> myinfor.values() #得到两个list
[‘python’, ‘qiwsir’, ‘qiwsir.github.io’]
>>> myinfor.keys()
[‘lang’, ‘name’, ‘site’]
>>> temp = zip(myinfor.values(),myinfor.keys()) #压缩成一个list，每个元素是一个tuple
>>> temp
[(‘python’, ‘lang’), (‘qiwsir’, ‘name’), (‘qiwsir.github.io’, ‘site’)]

>>> dict(temp) #这是函数dict()的功能，将上述列表转化为dictionary
{‘python’: ‘lang’, ‘qiwsir.github.io’: ‘site’, ‘qiwsir’: ‘name’}

enumerate

enumerate的详细解释，在《再深点，更懂list》中已经有解释，这里姑且复习。

>>> mylist = [“qiwsir”,703,”python”]
>>> new_list = []
>>> for i in range(len(mylist)):
… new_list.append((i,mylist[i]))

>>> new_list
[(0, ‘qiwsir’), (1, 703), (2, ‘python’)]

enumerate的作用就是简化上述操作：

>>> enumerate(mylist)
#出现这个结果，用list就能显示内容.类似的会在后面课程出现，意味着可迭代。
>>> list(enumerate(mylist))
[(0, ‘qiwsir’), (1, 703), (2, ‘python’)]

class enumerate(object)
| enumerate(iterable[, start]) -> iterator for index, value of iterable
|
| return an enumerate object. iterable must be another object that supports
| iteration. the enumerate object yields pairs containing a count (from
| start, which defaults to zero) and a value yielded by the iterable argument.
| enumerate is useful for obtaining an indexed list:
| (0, seq[0]), (1, seq[1]), (2, seq[2]), …
|
| methods defined here:
|
| getattribute(…)
| x.getattribute(‘name’) x.name
|
| iter(…)
| x.iter() iter(x)
|
| next(…)
| x.next() -> the next value, or raise stopiteration

data and other attributes defined here:
new =
t.new(s, …) -> a new object with type s, a subtype of t

Posted in 未分类