Python程序为我提供了一系列项目,而不是大多数/最不频繁的项目

时间:2014-10-01 03:19:36

标签: python list loops range

我使用wxFormBuilder创建了一个GUI,该GUI允许用户在列表中输入“商家访客”的名称,然后单击两个按钮之一,以返回业务中最频繁和最不频繁的访问者。不幸的是,当我点击按钮时,它会给我一系列的访问者,而不是最常/最不常访问者的名字。我附上了我创建的GUI的屏幕截图,以帮助为问题添加一点清晰度并附加我的代码。在此先感谢您的帮助。

enter image description here

代码:

import wx
import myLoopGUI

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):
        word_amount = range(self.listMyData.GetCount())
        numberofitems = self.listMyData.GetCount()
        unique_words = []
        for word in word_amount:
                if word not in unique_words:
                    unique_words += [word]
        word_frequencies = []
        for word in unique_words:
            word_frequencies += [float(word_amount.count(word))/len(word_amount)]

        max_index = 0
        frequent_words =[]
        for i in range(len(unique_words)):
            if word_frequencies[i] >= word_frequencies[max_index]:
                max_index = i
                frequent_words += [unique_words[max_index]]
        self.txtResults.Value = str(frequent_words)



myApp = wx.App(False)
myFrame = MyLoopFrame(None)
myFrame.Show()
myApp.MainLoop()

@Yep_It's_Me非常友好,可以提供另一种方式:

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):
        self.listMyData = []
        unique_names = set(self.listMyData)
        frequencies = {}
        for name in unique_names:
            if frequencies.get[name]:
                frequencies[name] += 1
            else:
                frequencies[name] = 0

        v = list(frequencies.values())
        k = list(frequencies.keys())
        self.txtResults.Value = k.index(max(v))


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

        v = list(frequencies.values())
        k = list(frequencies.keys())
        self.txtResults.Value = k.index(min(v))

myApp = wx.App(False)
myFrame = MyLoopFrame(None)
myFrame.Show()
myApp.MainLoop()

但现在我收到了一个Value错误:

ValueError:max()arg是一个空序列

关于这一行:

**self.txtResults.Value = k.index(max(v))**
没关系! @Yep_It's_Me提供了一个梦幻般的解决方案。非常感谢你。

1 个答案:

答案 0 :(得分:0)

range(self.listMyData.getCount())

会提供从0self.listMyData.getCount()的整数列表。

您想要计算self.listMyData是的唯一项目的出现次数吗?

更新:在评论中,self.listMyData是文本框,而不是名称列表,从文本框中获取名称列表,如this answer所示。更新了代码。

# Get the list of names from the textbox
name_list = 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

现在frequencies将成为名称的词典,其值为self.listMyData

中该名称的频率

然后你可以这样做:

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]

这将为counts分配一个整数列表,表示names中每个名称的频率。获取counts中的最大值和最小值的索引会告诉我们names中的索引,它们分别具有最频繁和最不频繁的名称。然后我们可以正常方式访问它们。