そのため、いくつかの異なるオプションのタイミングを計り、数回繰り返した後、次の戦略を思いつきました. 私は sets2 が勝者になると思っていましたが、listToSet2 はすべてのタイプのグループでより高速でした。
listFilter を除くすべての関数は同じ球場にあり、listFilter ははるかに低速でした。
import random
import collections
small = [[random.randint(1,25) for _ in range(5)] for i in range(100)]
medium = [[random.randint(1,250) for _ in range(5)] for i in range(1000)]
mediumLotsReps = [[random.randint(1,25) for _ in range(5)] for i in range(1000)]
bigGroups = [[random.randint(1,250) for _ in range(75)] for i in range(100)]
huge = [[random.randint(1,2500) for _ in range(5)] for i in range(10000)]
def sets(groups):
results = collections.defaultdict(set)
for group in groups:
for i in group:
for j in group:
if i is not j:
results[i].add(j)
return results
def listToSet(groups):
results = collections.defaultdict(list)
for group in groups:
for i,j in enumerate(group):
results[j] += group[:i] + group[i:]
return {k:set(v) for k, v in results.iteritems()}
def listToSet2(groups):
results = collections.defaultdict(list)
for group in groups:
for i,j in enumerate(group):
results[j] += group
return {k:set(v)-set([k]) for k, v in results.iteritems()}
def sets2(groups):
results = collections.defaultdict(set)
for group in groups:
for i in group:
results[i] |= set(group)
return {k:v - set([k]) for k, v in results.iteritems()}
def listFilter(groups):
results = collections.defaultdict(list)
for group in groups:
for i,j in enumerate(group):
filteredGroup = group[:i] + group[i:]
results[j] += ([k for k in filteredGroup if k not in results[j]])
return results