假設你有蟒蛇2.6或更高:
>>> from itertools import combinations
>>>
>>> data = dict(
... list1 = set(list("alphabet")),
... list2 = set(list("fiddlesticks")),
... list3 = set(list("geography")),
... list4 = set(list("bovinespongiformencephalopathy")),
...)
>>>
>>> variations = {}
>>> for i in range(len(data)):
... for v in combinations(data.keys(),i+1):
... vsets = [ data[x] for x in v ]
... variations[tuple(sorted(v))] = reduce(lambda x,y: x.intersection(y), vsets)
...
>>> for k,v in sorted(variations.items(),key=lambda x: (len(x[0]),x[0])):
... print "%r\n\t%r" % (k,v)
...
('list1',)
set(['a', 'b', 'e', 'h', 'l', 'p', 't'])
('list2',)
set(['c', 'e', 'd', 'f', 'i', 'k', 'l', 's', 't'])
('list3',)
set(['a', 'e', 'g', 'h', 'o', 'p', 'r', 'y'])
('list4',)
set(['a', 'c', 'b', 'e', 'g', 'f', 'i', 'h', 'm', 'l', 'o', 'n', 'p', 's', 'r', 't', 'v', 'y'])
('list1', 'list2')
set(['e', 'l', 't'])
('list1', 'list3')
set(['a', 'h', 'e', 'p'])
('list1', 'list4')
set(['a', 'b', 'e', 'h', 'l', 'p', 't'])
('list2', 'list3')
set(['e'])
('list2', 'list4')
set(['c', 'e', 'f', 'i', 'l', 's', 't'])
('list3', 'list4')
set(['a', 'e', 'g', 'h', 'o', 'p', 'r', 'y'])
('list1', 'list2', 'list3')
set(['e'])
('list1', 'list2', 'list4')
set(['e', 'l', 't'])
('list1', 'list3', 'list4')
set(['a', 'h', 'e', 'p'])
('list2', 'list3', 'list4')
set(['e'])
('list1', 'list2', 'list3', 'list4')
set(['e'])
你需要顯示它的圖形? – Khelben 2010-02-11 10:57:17
生成數據和工會很容易使用'set' – Khelben 2010-02-11 10:57:52
我不需要圖形,因爲Venny對我來說很容易,我只想提取列表。 – Einar 2010-02-11 14:45:34