ValueError: max() arg is an empty sequence

2024/11/19 19:59:55

I've created a GUI using wxFormBuilder that should allow a user to enter the names of "visitors to a business" into a list and then click one of two buttons to return the most frequent and least frequent visitors to the business.

I created an earlier version that, unfortunately, gave me the range of visitors, rather than the name of the most/least frequent visitor. I've attached a screenshot of the GUI I've created to help add a little clarity to the issue ( https://i.stack.imgur.com/6GHbK.jpg ).

A new code version takes a different tack than the earlier version, and I can't get it to throw anything. Instead, I keep receiving this error:

ValueError: max() arg is an empty sequence

In relation to this line:

self.txtResults.Value = k.index(max(v))

import wx
import myLoopGUI
import commandsclass 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] += 1else:frequencies[name] = 0v = 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] += 1else:frequencies[name] = 0v = 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()
Answer

Pass a default value which can be returned by max if the sequence is empty:

max(v, default=0)
https://en.xdnf.cn/q/26327.html

Related Q&A

How to write native newline character to a file descriptor in Python?

The os.write function can be used to writes bytes into a file descriptor (not file object). If I execute os.write(fd, \n), only the LF character will be written into the file, even on Windows. I would …

pandas combine two columns with null values

I have a df with two columns and I want to combine both columns ignoring the NaN values. The catch is that sometimes both columns have NaN values in which case I want the new column to also have NaN. H…

Python equivalent of zip for dictionaries

If I have these two lists:la = [1, 2, 3] lb = [4, 5, 6]I can iterate over them as follows:for i in range(min(len(la), len(lb))):print la[i], lb[i]Or more pythonicallyfor a, b in zip(la, lb):print a, bW…

Can I prevent fabric from prompting me for a sudo password?

I am using Fabric to run commands on a remote server. The user with which I connect on that server has some sudo privileges, and does not require a password to use these privileges. When SSHing into th…

Managing connection to redis from Python

Im using redis-py in my python application to store simple variables or lists of variables in a Redis database, so I thought it would be better to create a connection to the redis server every time I n…

Writing a Python extension in Go (Golang)

I currently use Cython to link C and Python, and get speedup in slow bits of python code. However, Id like to use goroutines to implement a really slow (and very parallelizable) bit of code, but it mus…

Non-global middleware in Django

In Django there is a settings file that defines the middleware to be run on each request. This middleware setting is global. Is there a way to specify a set of middleware on a per-view basis? I wan…

I want to replace single quotes with double quotes in a list

So I am making a program that takes a text file, breaks it into words, then writes the list to a new text file.The issue I am having is I need the strings in the list to be with double quotes not singl…

How can I get stub files for `matplotlib`, `numpy`, `scipy`, `pandas`, etc.?

I know that the stub files for built-in Python library for type checking and static analysis come with mypy or PyCharm installation. How can I get stub files for matplotlib, numpy, scipy, pandas, etc.?…

Pipfile.lock out of date

Im trying to deploy a large django project to heroku. I installed Heroku CLI, logged in, created an app and ran:git push heroku masterI have a Pipfile and requirements.txt already set up. I added a run…