这是一个展示一种方法的交互式会话
bash-3.2$ python
Python 2.7.12 (default, Nov 29 2016, 14:57:54)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a, b = 101011, ('a','b','c','d','e','f','g')
>>> a, b
(101011, ('a', 'b', 'c', 'd', 'e', 'f', 'g'))
>>> list(str(a))
['1', '0', '1', '0', '1', '1']
>>> zip(list(str(a)),b)
[('1', 'a'), ('0', 'b'), ('1', 'c'), ('0', 'd'), ('1', 'e'), ('1', 'f')]
>>> filter(lambda x:x[0]=='1', zip(list(str(a)),b))
[('1', 'a'), ('1', 'c'), ('1', 'e'), ('1', 'f')]
>>> [x[1] for x in filter(lambda x:x[0]=='1', zip(list(str(a)),b))]
['a', 'c', 'e', 'f']
我们可以使用解构赋值使其更简洁
>>> [y for x,y in zip(list(str(a)),b) if x=='1']
['a', 'c', 'e', 'f']