如何在Python 3.2中添加列表中的项目频率数? [英] How to append the number of item frequencies in a list in Python 3.2?

查看:130
本文介绍了如何在Python 3.2中添加列表中的项目频率数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚编程,python是我学到的第一种语言。

I'm new to programming and python is the first language I've learned.

我想问的问题是你如何计算项目的频率在列表
中,它们按照PARTY_INDICES的顺序相加?在我的情况下。

The question I want to ask is how do you count the frequency of items in a list so they add up in order with "PARTY_INDICES"? in my case that is.

这是我需要做的一个docstring:

''' (list of str) -> tuple of (str, list of int) 
votes is a list of single-candidate ballots for a single riding. 
Based on 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 votes for each party in 
the order specified in PARTY_INDICES.

>>> voting_plurality(['GREEN', 'GREEN', 'NDP', 'GREEN', 'CPC']) 
('GREEN', [1, 3, 0, 1])
'''

由于PARTY_INDICES = [NDP_INDEX,GREEN_INDEX,LIBERAL_INDEX,CPC_INDEX]
这产生一个元组获胜方(在这种情况下是GREEN)和
频率的列表,其中[1,3,0,1]

Since PARTY_INDICES = [NDP_INDEX, GREEN_INDEX, LIBERAL_INDEX, CPC_INDEX] This produces a tuple of the winning party (In this case 'GREEN') and the list of frequencies, where [1, 3, 0, 1]

是全局变量,列表和字典:

#  The indices where each party's data appears in a 4-element list.
NDP_INDEX = 0
GREEN_INDEX = 1
LIBERAL_INDEX = 2
CPC_INDEX = 3

# A list of the indices where each party's data appears in a 4-element list.
PARTY_INDICES = [NDP_INDEX, GREEN_INDEX, LIBERAL_INDEX, CPC_INDEX]

# A dict where each key is a party name and each value is that party's index.
NAME_TO_INDEX = {
  'NDP': NDP_INDEX,
  'GREEN': GREEN_INDEX,
  'LIBERAL': LIBERAL_INDEX,
  'CPC': CPC_INDEX
}

# A dict where each key is a party's index and each value is that party's name.
INDEX_TO_NAME = {
  NDP_INDEX: 'NDP',
  GREEN_INDEX: 'GREEN',
  LIBERAL_INDEX: 'LIBERAL',
  CPC_INDEX: 'CPC'
}

这是我的工作:

def voting_plurality(votes):
    my_list = []
    my_dct = {}
    counter = 0
    for ballot in votes:
        if (ballot in my_dct):
            my_dct[ballot] += 1
        else:
            my_dct[ballot] = 1

    if (my_dct):
        my_dct = my_dct.values()
        new_list = list(my_dct)

    return (max(set(votes), key = votes.count), new_list)

它返回:

>>> voting_plurality(['GREEN', 'GREEN', 'NDP', 'GREEN', 'CPC'])
('GREEN', [1, 1, 3])

但是我希望它也包括没有投票的聚会,并且与PARTY_INDICES [1,3,0,1]

But I want it to also include the party with no votes and is in order with PARTY_INDICES [1, 3, 0, 1]

我的代码可能看起来像废话,但我真的困惑和困惑。

My code may look like nonsense, but I'm really stuck and confused.

我也不能 IMPORT

Also I cannot IMPORT anything.

推荐答案

有两个主要问题。第一个是你必须捕捉到零,但由于没有投票给自由党,零将不被反映。

There are two main problems you have. The first is that you have to capture the zero, but since there are no votes for the "liberal party", the zero will not be reflected.

提示可能您要初始化您的字典?

Tip Maybe you want to initialize your dictionary?

第二个问题是您调用的dict.values()不会有任何排序。您需要使用字典,并且 PARTY_INDICES 创建正确排序的数字列表。

The second problem is that you are calling dict.values() which will not be in any sort of order. You need to use the dictionary, and the PARTY_INDICES to create the correctly ordered list of numbers.

提示也许您可以在 PARTY_INDICIES 列表

中引用字典中的键及其各自的位置看看你能否想出这些提示,并更新你的问题。如果你不能,我相信有人会最终发布一个完整的答案。

See if you can come up with something given these tips, and update your question. If you can't, I'm sure someone will post a full answer eventually.

看到已经4个小时了 - 这是一个解决方案:

Seeing as it has been 4 hours - here is a solution:

def voting_plurality(votes):
    sums = dict(zip(INDEX_TO_NAME.values(), [0] * len(INDEX_TO_NAME)))
    for vote in votes:
        if vote in sums:
            sums[vote] += 1
        else:
            print "Bad vote: %s" % vote
    votes_by_index = sorted([(NAME_TO_INDEX[k], v) for k, v in sums.items()])
    votes_by_rank = sorted(votes_by_index, key=lambda x: x[1], reverse=True)
    votes_by_parts = [item[1] for item in votes_by_index]
    highest_votes = INDEX_TO_NAME[votes_by_rank[0][0]]
    return (highest_votes, votes_by_parts)

这篇关于如何在Python 3.2中添加列表中的项目频率数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆