Geeks/Nerds/Fellow Pythonists lend me your ears!
I wrote a script in Python 2.7 that uses the set() function. This is a built in data structure for only storing unique values. However I am running the script in a different environment. I know I should of coded in the same environment as the production environment. So in the production environment there is an ancient version of python running. From what I understand this is Python 2.2. Also, this Python interpreter is running on a Java Virtual Machine (JVM) which makes this Jython. Very cool. :) Anyway for whatever reason the set() data structure is not included in this version of Python. Is there anyway I can create my own set using a list and then looking for only unique values? This type of algorithm writing is currently over my head. Could someone point me in a helpful direction?
When running this code I see that the version is Python 2.2.
import sys
print sys.version_info
This code returns: (2, 2, 1, 'final', 0)
My code that throws the error is:
machine_set = set() #create a set, an empty set at that
The error I receive is as follows:
<me@linuxbox ~>$ java -cp $WEBLOGIC weblogic.WLST lolcats.py
Initializing WebLogic Scripting Tool (WLST) ...
Welcome to WebLogic Server Administration Scripting Shell
Type help() for help on available commands
Problem invoking WLST - Traceback (innermost last):
File "/home/oracle/lolcats.py", line 18, in ?
TypeError: set() takes at least 2 arguments (0 given)
First, you may not be stuck on Jython 2.2. It looks like 2.2.1 is what comes with your WLST, but Google suggests that you may be able to use WLST with Jython 2.7. If you can use 2.7, that's likely to be a much nicer environment than 2.2. (It's still pretty dated by non-Jython standards, but it beats the heck out of 2.2.)
If that's not viable, still, don't use a list. Back when set
didn't exist, the way to do sets was with a dict
you'd ignore the values of. Same time complexity as a set
, unlike what you'd get if you tried to use list
for the job. The old sets
module that came before the built-in set
type was actually implemented as a wrapper around dicts, and if you were on 2.3, I'd recommend sets.Set
, but you're on 2.2.
# Add a value
d[val] = None
# Remove a value
del d[val]
# Check if a value exists (good thing it's not 2.1, or you'd need has_key)
key in d