【问题标题】:How to shorten this code without using bunch of if statements如何在不使用大量 if 语句的情况下缩短此代码
【发布时间】:2012-11-24 22:18:52
【问题描述】:
CONSTANTS:

NDP_INDEX = 0
GREEN_INDEX = 1
LIBERAL_INDEX = 2
CPC_INDEX = 3

各方数据出现在 4 元素列表中的索引分布。

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

我不确定如何使用辅助函数来缩短它,因为我们不允许创建新常量(通用变量)而只能创建局部变量

【问题讨论】:

    标签: python python-3.x python-2.7


    【解决方案1】:

    试试这个:

    for sub_list in range_votes:
       for i in range(4):
           PARTY_INDICES[i] += sub_list[i]
    return ( {PARTY_INDICES[0] : 'NDP', PARTY_INDICES[1] : 'GREEN', ...}[max(PARTY_INDICES)], 
             PARTY_INDICES )
    

    只要我愿意,就这么短;-)

    【讨论】:

    • 您可以为 i in range(4): PARTY_INDICES[i] += sub_list[0] 做一秒钟来替换分配。 ;-)
    【解决方案2】:

    您不需要所有这些 dicts 和 range 变量并定义/枚举看起来的东西。这是少一点 C 多一点 python:

    votes=[[5, 4, 1, 4], [3, 2, 2, 5], [3, 3, 1, 4,]]
    parties=['NDP','GREEN','LIBERAL','CPC']
    
    def voting_range(v):
      totals = [sum(x) for x in zip(*v)] # transpose row/col of list of lists
      pname = parties[totals.index(max(totals))]
      return (pname,totals)
    
    >>> voting_range(votes)
    ('CPC',[11, 9, 4, 13])
    

    这里发生了一些神奇的 python 事情。 *v 解包列表列表,并将它们作为单独的参数提供给 zip,它返回一个迭代,它给出列表中每个参数的第一个元素,然后是列表中每个参数的第二个元素,依此类推.这具有转置矩阵的效果(交换行和列)。在这种形式中,简单地将每个列表相加将是投票总数。括在括号中的for x in 语法是list comprehension,这是另一个很棒的python 功能,可以有效地从可迭代对象创建列表。最后,index 是一个 list method,它将返回具有指定值的第一个元素的索引,在本例中为最大值。由于 v 的元素应该具有与参与方数量相同的长度,这也将作为参与方列表的索引。

    【讨论】:

    猜你喜欢
    • 2012-04-06
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多