Re: Checking length of each argument - seems like I'm fighting Python
* Brendan ([EMAIL PROTECTED]) wrote: [...] > Is there a simpler way to check that either all arguments are scalars, > or all are lists of the same length? Is this a poor way to structure > things? Your advice is appreciated Disclaimer: I am new to python, so this may be a bad solution. import types def __init__(self,x,y,z): isOK = False if ( (type(x) == types.IntType) and (type(y) == types.IntType) and (type(z) == types.IntType) ): isOK = True if ( (type(x) == types.ListType) and (type(x) == types.ListType) and (type(x) == types.ListType) ): if ( (len(x) == len(y)) and (len(x) == len(z)) ): isOK = True HTH, mike -- http://mail.python.org/mailman/listinfo/python-list
Re: Want to reduce steps of an operation with dictionaries
* [EMAIL PROTECTED] ([EMAIL PROTECTED]) wrote:
> Hello:
> I have next dictionaries:
> a={'a':0, 'b':1, 'c':2, 'd':3}
> b={'a':0, 'c':1, 'd':2, 'e':3}
> I want to put in a new dictionary named c all the keys that are in b
> and re-sequence the values. The result I want is:
> c={'a':0, 'c':1, 'd':2}
> How can I do this with one line of instruction?
>
> I attempted the next but the output is not the expected:
> c=dict([(k,v) for v,k in enumerate(a) if b.has_key(k)])
> erroneously (for me) gets:
> {'a': 0, 'c': 2, 'd': 3}
I am not 100% I understand your questions, but k,v are being pulled from
a, try:
c=dict([(k,b[k]) for v,k in enumerate(a) if b.has_key(k)])
mike
--
http://mail.python.org/mailman/listinfo/python-list
Re: Iterating over several lists at once
* at ([EMAIL PROTECTED]) wrote: > Sorry for breaking into this thread, but I agree completely that any > unnecessary indentations should be avoided. For the same reason I advocate > that the following syntax should work: > > for x in some_list if some_condition: > ... code ... > > in stead of > > for x in some_list > if some_condition: > ... code ... It is possible to avoid the extra level of indentaion, but I think it's much less readable than the 2-level verbose expresion: >>> a [1, 2, 3, 4, 5, 6, 7] >>> for odd in (num for num in a if num % 2 == 1): ... print odd ... 1 3 5 7 there is also continue, which I think is a good compromise: >>> for num in a: ... if num % 2 == 0: ... continue ... print num ... 1 3 5 7 HTH (and not lead astray), mike -- http://mail.python.org/mailman/listinfo/python-list
Re: Can optparse do dependencies?
* Bob ([EMAIL PROTECTED]) wrote:
> I'd like to setup command line switches that are dependent on other
> switches, similar to what rpm does listed below. From the grammar below
> we see that the "query-options" are dependent on the query switch,
> {-q|--query}. Can "optparse" do this or do I have to code my own
> "thing"? Thanks.
After you pull your options and arguments from optparse, just check for
them, eg:
parser = optparse.OptionParser()
...
(options, args) = parser.parse_args()
if (option.q and not option.p):
parser.error("you must use p to use q")
The meaning of -b doesn't change when it follows -a, but if you want
that, it is doable by extending optparse. There is even an example in
the very good "extending optik"[1] documentation available off the
sf.net site:
http://optik.sourceforge.net/
regards,
mike
[1]: optparse is also known as optik
--
http://mail.python.org/mailman/listinfo/python-list
