下面是我想要完成的一個典型場景: 約26名球員名冊 第1周:26的18出現。我組成9支球隊 第2周:26場比賽中有22場出現。我組成11支球隊,但從第1周開始沒有重複。 第3周:26中有10人出現。我組成了5支球隊,但從第1周或第2周沒有重複,等等。
儘管沒有過去應用過濾器,但最多有12名球員(需要25秒),但是超過12的球員無法忍受長時間,我預計大約有24名球員。 程序選擇第一組有效的球隊,然後結束。隨着附加的past__teams過濾器應用 - 程序的全部重點 - 它將不得不運行更長時間。 有沒有一種替代方法來解決這個問題,或者是一種可以大幅提高速度的方法?
from itertools import combinations, chain
import pickle, random
'''the'past__teams.py' file was previously created as follows:
output = open('past__teams.pkl', 'wb')
pickle.dump(past__teams, output)
output.close()'''
players = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
random.shuffle(players)
#load the past__teams file into the program
pkl_file = open('past__teams.pkl', 'rb')
past__teams = pickle.load(pkl_file)
pkl_file.close()
all_pair_combinations= []
tonights_teams = []
for c in combinations(players, 2):
c = list(c)
all_pair_combinations.append(c)
def dupe_test(L):
if len(L) != len(set(L)):
return True
return False
y = 0
for t in combinations(all_pair_combinations, int(len(players)/2)):
t = list(t)
tt =list(chain(*t))
y = y +1
if dupe_test(tt) == False:
tonights_teams =t
break
print()
print ("Tonights_teams: ",y,tonights_teams)
讓我們來檢查「X-Y」問題狀態:這是什麼目的?你是否想爲2N玩家建立一個完整的循環配對?這是一個稍微不同的問題,但已知的解決方案。如果您需要按照您的描述所暗示的那樣以不太有序的方式處理算法,那麼我們會遇到一個稍微困難的問題。 – Prune
請分享你的嘗試,即使他們不工作。這正是Stack Overflow的作用。 –