频率的最大值和最小值都返回相同的值

时间:2014-10-02 01:15:57

标签: python loops max frequency min

我使用wxFormBuilder创建了一个GUI,该GUI允许用户在列表中输入“商家访客”的名称,然后单击两个按钮之一,以返回业务中最频繁和最不频繁的访问者。以下是GUI:http://imgur.com/XJnvo0U

不幸的是,Most和Least按钮都抛出相同的值(Most)。我认为它与频率如何加权输入有关,也许与clkFindMost和clkFindLeast事件的定义重叠有关。任何帮助将不胜感激。

**编辑:我对Python非常陌生,学习曲线是STEEP

import wx
import myLoopGUI
import commands

class MyLoopFrame(myLoopGUI.MyFrame1):
    def __init__(self, parent):
        myLoopGUI.MyFrame1.__init__(self, parent)

    def clkAddData(self,parent):
        if len(self.txtAddData.Value) != 0:
            try:
                myname = str(self.txtAddData.Value)
                self.listMyData.Append(str(myname))
            except:
                wx.MessageBox("This has to be a name!")            
        else:
            wx.MessageBox("This can't be empty")




    def clkFindMost(self, parent):
        name_list = set(self.listMyData.GetStrings())
        unique_names = set(name_list)
        frequencies = {}
        for name in unique_names:
            if frequencies.get(name):
                frequencies[name] += 1
            else:
                frequencies[name] = 0

        counts = list(frequencies.values())
        names = list(frequencies.keys())
        max_count_index = counts.index(max(counts))
        min_count_index = counts.index(min(counts))
        most_frequent = names[max_count_index]
        least_frequent = names[min_count_index]
        self.txtResults.Value = most_frequent



    def clkFindLeast(self, parent):
        name_list = set(self.listMyData.GetStrings())
        unique_names = set(name_list)
        frequencies = {}
        for name in unique_names:
            if frequencies.get(name):
                frequencies[name] += 1
            else:
                frequencies[name] = 0

        counts = list(frequencies.values())
        names = list(frequencies.keys())
        max_count_index = counts.index(max(counts))
        min_count_index = counts.index(min(counts))
        most_frequent = names[max_count_index]
        least_frequent = names[min_count_index]
        self.txtResults.Value = least_frequent

    def clkClear (self, parent):
        self.txtResults.SetValue("")

#Needed to ensure the program runs        
myApp = wx.App(False)
myFrame = MyLoopFrame(None)
myFrame.Show()
myApp.MainLoop()

1 个答案:

答案 0 :(得分:0)

编辑:在我原来的回答中,我专注于错误的问题。

首先,迭代一组唯一名称来确定频率是没有意义的。您应该迭代所有名称,例如:

    name_list = self.listMyData.GetStrings() # presuming this is an iterable
    frequencies = {}
    for name in name_list:
        if frequencies.get(name):
            frequencies[name] += 1
        else:
            frequencies[name] = 0

即使你的任务不允许,我也会留下这个用于启发:
collections.defaultdict中提供了一个优雅的解决方案,可以在collections.defaultdict中创建一个密钥,如果密钥不存在则会创建密钥,如果密钥存在则会增加密钥。由于dict子类dict,您稍后在代码中使用的import collections as co # at top of script # ... name_list = self.listMyData.GetStrings() # presuming this is an iterable frequencies = co.defaultdict(int) for name in name_list: frequencies[name] += 1 方法仍然可以使用。

{{1}}