CONSTANTS:
NDP_INDEX = 0
GREEN_INDEX = 1
LIBERAL_INDEX = 2
CPC_INDEX = 3
每個方的數據出現在4元素列表中的索引dist。如何在不使用一堆if語句的情況下縮短此代碼
PARTY_INDICES = [NDP_INDEX, GREEN_INDEX, LIBERAL_INDEX, CPC_INDEX]
一個字典,其中每個鍵是一個派對名稱,每個值是該派對的索引。
NAME_TO_INDEX = {'NDP': NDP_INDEX,
'GREEN': GREEN_INDEX,'LIBERAL': LIBERAL_INDEX,'CPC': CPC_INDEX}
一個字典,其中每個鍵是一方的索引,每個值是該方的名稱。
INDEX_TO_NAME = {NDP_INDEX: 'NDP',GREEN_INDEX: 'GREEN', LIBERAL_INDEX:
'LIBERAL',CPC_INDEX: 'CPC'}
def voting_range(range_votes):
'''
(list of list of int) -> tuple of (str, list of int)
#range_votes is a list of integers of range ballots for a single
#riding; the order of the inner list elements corresponds to the order
#of the parties in PARTY_INDICES.Based on range_votes, return a tuple
#where the first element is the name of the party winning the seat and
#the second is a list with the total range_votes for each party in the
#order specified in PARTY_INDICES.
>>> voting_range([[5, 4, 1, 4], [3, 2, 2, 5], [3, 3, 1, 4,]])
('CPC', [11, 9, 4, 13])
'''
NDP_count = 0
GREEN_count = 0
LIBERAL_count = 0
for sub_list in range_votes:
NDP_count += sub_list[0]
GREEN_count += sub_list[1]
LIBERAL_count += sub_list[2]
CPC_count += sub_list[3]
PARTY_INDICES[0] = NDP_count
PARTY_INDICES[1] = GREEN_count
PARTY_INDICES[2] = LIBERAL_count
PARTY_INDICES[3] = CPC_count
winner = max(NDP_count, GREEN_count, LIBERAL_count, CPC_count)
if winner == NDP_count:
return 'NDP', PARTY_INDICES
elif winner == GREEN_count:
return 'GREEN', PARTY_INDICES
elif winner == LIBERAL_count:
return 'LIBERAL', PARTY_INDICES
elif winner == CPC_count:
return 'CPC', PARTY_INDICES
我不知道如何縮短這甚至利用輔助功能,因爲我們不允許創建新的常數(通用變量),但只是局部變量
您可以在範圍(4)中爲我做第二個:PARTY_INDICES [i] + = sub_list [0]以替換分配。 ;-) – GaretJax
@GaretJax,的確如此:) – StoryTeller