Python newb here. I m trying to count the number of letter “a”s in a given string. Code is below. It keeps returning 1 instead 3 in string “banana”. Any input appreciated. Answer The other answers show what’s wrong with your code. But there’s also a built-in way to do this, if you weren’t just doing this for an exercise:
What do the * (star) and ** (double star) operators mean in a function call?
In code like zip(*x) or f(**k), what do the * and ** respectively mean? How does Python implement that behaviour, and what are the performance implications? See also: Expanding tuples into arguments. Please use that one to close questions where OP needs to use * on an argument and doesn’t know it exists. Answer A single star * unpacks a
Longest common substring from more than two strings
I’m looking for a Python library for finding the longest common sub-string from a set of strings. There are two ways to solve this problem: using suffix trees using dynamic programming. Method implemented is not important. It is important it can be used for a set of strings (not only two strings). Answer These paired functions will find the longest
read subprocess stdout line by line
My python script uses subprocess to call a linux utility that is very noisy. I want to store all of the output to a log file and show some of it to the user. I thought the following would work, but the output doesn’t show up in my application until the utility has produced a significant amount of output. The
socket.error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions
I’m trying to create a custom TCP stack using Python 2.6.5 on Windows 7 to serve valid http page requests on port 80 locally. But, I’ve run into a snag with what seems like Windows 7 tightened up security. This code worked on Vista. Here’s my sample code: C:python>python TestServer.py Traceback (most recent call last): File “TestServer.py”, line 19, in
Django datetime issues (default=datetime.now())
I have the below db model: I add a new instance by using the below: My issue: all records in database have the same value in date field, which is the date of the first payment. After the server restarts, one record has the new date and the other records have the same as the first. It looks as if
Python: get default gateway for a local interface/ip address in linux
On Linux, how can I find the default gateway for a local ip address/interface using python? I saw the question “How to get internal IP, external IP and default gateway for UPnP”, but the accepted solution only shows how to get the local IP address for a network interface on windows. Thanks. Answer For those people who don’t want an
How to pass elements of a list as arguments to a function?
I’m building a simple interpreter in python and I’m having trouble handling differing numbers of arguments to my functions. My current method is to get a list of the commands/arguments as follows. Then to execute com, I check to see if it is in my dictionary of command-> code mappings and if it is I call the function I have
Python logger dynamic filename
I want to configure my Python logger in such a way so that each instance of logger should log in a file having the same name as the name of the logger itself. e.g.: I want to keep it generic and dont want to hardcode all kind of logger names I can have. Is that possible? Answer
Importing modules on portable python
I am running PortablePython_1.1_py2.6.1 on a USB stick. My code relies on some modules that are not preinstalled. Does anyone know whether it is possible to add new modules to a portable python installation? Simply copying in folders into site-lib does not seem to work. Answer What does import sys; print sys.path say? It should be the list of directories