我正在為我的第一個主要專案創建一個組排序功能 - 為我的合作伙伴在她的作業(老師)的課程組制定時間表。有20個字母。每天可以發送 4 封信,因此所有 20 封信應該每 5 天發送一次。我創建了 5 個較小的組(串列),其中包含 4 個字母,嵌套在名為“all_groups”的串列中。每 5 天(一周左右),組的順序由 insert(0, group.pop()) 輪換一次。這沒有問題。但是,我也想自己輪換 5 個嵌套串列并保持不變。這是不起作用的部分。
我有幾個助手全域計數器來完成這項作業。
每次呼叫該函式時,我都會為每個計數器加 1。計數器 1 - 隨機器。如果 randomizer == 21,則輪換 5 個主要組。
計數器 2 - 總變化。這會計算呼叫函式的總次數。我使用它來避免使用條件旋轉任何嵌套串列的第一次復飛。
計數器 3 - group_index - 此計數器從 0-4 開始,讓函式選擇在重置回 0 之前應該呼叫 5 個組中的哪一個。
計數器 4 - group_changes。這個計數器從 0 到 3 計數,因為每個串列中有 4 個字母。一旦所有 4 個字母都被旋轉,如果 group_changes == 4,它應該重置為 0,并將 group_index 增加 1。
主串列中的 5 個子組的旋轉和索引的更改似乎有效,并且每個單獨的字母旋轉一次并正確列印,但任何進一步的回圈傾向于將內部串列重置為其原始狀態,而不是保持它們被修改。我很困惑如何解決這個問題。
另外:如果我每次都使用 randomize_group 函式而不是 rotate_group 函式(如果 group_ranzomier == 21),則嵌套串列有時會在內部旋轉。為了調度目的,我想隨機化而不是輪換 5 個串列。我不知道為什么隨機化 5 個串列會改變內部旋轉函式的行為。
我對這部分專案的代碼如下,我對代碼進行了評論以描述它暫時在做什么:
group_randomizer = 0 #this is for randomizing the subgroup order each loop of 20 groups
group_change = 0 #tracks individual swaps
group_index = 0 #tracks each subgroup of 5 groups of 4
total_changes = 0 #tracks total group swaps
all_groups = [['A', 'B', 'C', 'D'],
['E', 'F', 'G', 'H'],
['I', 'J', 'K', 'L'],
['M', 'N', 'O', 'P'],
['Q', 'R', 'S', 'T']]
def main():
#this entire block of code is for testing purposes to see what the output is
print(all_groups[group_index])
for i in range(20):
print(find_group(all_groups), end='')
print("")
print(tabulate(all_groups))
for i in range(8):
for i in range(20):
print(find_group(all_groups), end='') #printing the entire 20 group loop in one line
print(all_groups[group_index]) #to see what the first line is
print(tabulate(all_groups)) #for testing purposes, to read table
print("")
def rotate_group(group):
group.insert(0, group.pop())
return group
# def randomize_group(group):
# random.shuffle(group)
# return group
def find_group(all_groups):
global group_randomizer
global group_change
global group_index
global total_changes
group_randomizer = 1
if group_randomizer == 21:
rotate_group(all_groups) #rotates the 5 subgroups of 4 after each letter has gone through
group_randomizer = 0
if total_changes < 19: #to keep groups un-randomized the first loop of 20 groups. base case
rotated = all_groups[group_index][group_change]
else: #begin rotating the letters in the subgroups. ABCD -> DABC -> CDAB -> BCDA -> ABCD
all_groups = rotate_group(all_groups[group_index])
rotated = all_groups
total_changes = 1 #increment total changes
group_change = 1 #increment a rotation counter.
if group_change == 4: #if the rotation counter reaches 4, reset it and increment the subgroup index
group_index = 1
group_change = 0
if group_index == 5: #if the subgroup index reaches 5, reset it to return back to the first subgroup
group_index = 0
return rotated[0] #return the current rotated letter
uj5u.com熱心網友回復:
您的代碼確實有效,只需要進行一次更改。我不確定你是怎么稱呼它的,所以我只是回圈呼叫了 21 次。我所做的唯一更改是在else條款中。您正在將all_groups子串列分配給all_groups. 我剛剛添加了適當的索引。您將所有分配all_groups給rotated. 我剛剛添加了適當的索引。
考慮到您沒有提及錯誤,并且您聲稱您的代碼不起作用,我將假設您呼叫此函式一次,但您的代碼all_groups直到第 20 次才更改。如果您達到第 20 次,您將收到一條錯誤訊息,指出字串值沒有插入方法。
group_randomizer = 0 #this is for randomizing the subgroup order each loop of 20 groups
group_change = 0 #tracks individual swaps
group_index = 0 #tracks each subgroup of 5 groups of 4
total_changes = 0 #tracks total group swaps
all_groups = [['A', 'B', 'C', 'D'],
['E', 'F', 'G', 'H'],
['I', 'J', 'K', 'L'],
['M', 'N', 'O', 'P'],
['Q', 'R', 'S', 'T']]
def rotate_group(group):
group.insert(0, group.pop())
return group
def find_group(all_groups):
global group_randomizer
global group_change
global group_index
global total_changes
group_randomizer = 1
if group_randomizer == 21:
rotate_group(all_groups) #rotates the 5 subgroups of 4 after each letter has gone through
group_randomizer = 0
if total_changes < 19: #to keep groups un-randomized the first loop of 20 groups. base case
rotated = all_groups[group_index][group_change]
else: #begin rotating the letters in the subgroups. ABCD -> DABC -> CDAB -> BCDA -> ABCD
#was all_groups = rotate_group(all_groups[group_index])
all_groups[group_index] = rotate_group(all_groups[group_index])
#was rotated = all_groups
rotated = all_groups[group_index]
total_changes = 1 #increment total changes
group_change = 1 #increment a rotation counter.
if group_change == 4: #if the rotation counter reaches 4, reset it and increment the subgroup index
group_index = 1
group_change = 0
if group_index == 5: #if the subgroup index reaches 5, reset it to return back to the first subgroup
group_index = 0
return rotated[0] #return the current rotated letter
#this is how I called the script
#you didn't provide your method
for i in range(21):
find_group(all_groups)
print(all_groups)
uj5u.com熱心網友回復:
我很肯定,最好改變解決問題的方法以獲得更清晰的解決方案。例如,我建議為“all_groups”和“group”物體考慮一個類,并為物件提供相關的旋轉方法。無論如何,如果我們保持代碼邏輯不變,我會發現 2 個錯誤:
- 一個子組被分配給 all_groups
# all_groups = rotate_group(all_groups[group_index])
all_groups[group_index] = rotate_group(all_groups[group_index])
- 對于 total_changes < 19,變數“rotated”被分配了一個 str 型別的字母,并進一步分配了一個串列型別的子組
它可以固定在回傳線中。
# return rotated[0]
return rotated[0] if type(rotated) == list else rotated #return the current rotated letter
編輯: 另外,我認為如果代碼是為了獲得前 20 次的所有 20 個后續字母,而不是將 total_changes 比較為 20 而不是 19
if total_changes < 20: #to keep groups un-randomized the first loop of 20 groups. base case
rotated = all_groups[group_index][group_change]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/527773.html
