- 论坛徽章:
- 0
|
class Permutation:
def __init__(self, justalist):
self._data = justalist[:]
self._sofar = []
def __iter__(self):
return self.next()
def next(self):
for elem in self._data:
if elem not in self._sofar:
self._sofar.append(elem)
if len(self._sofar) == len(self._data):
yield self._sofar[:]
else:
for v in self.next():
yield v
self._sofar.pop()
a=[1,2,3,4]
for i in Permutation(a):
print i
刚刚从http://ttsiodras.googlepages.com/index.html看到的,很不错,用到了yield和generator的东西,值得借鉴。
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/46672/showart_2069243.html |
|