0

I'm using list comprehension to find specific datasets within a PyTable. However when trying to combine with arguments from argparser it returns no values.

Here is the section of code:

if args.Scount:
    print args.Scount, args.Scount[0], args.Scount[1]

    print 
    print [row['GRBname'] for row in table.iterrows() if args.Scount[0] <= row['SCounts'] <= args.Scount[1]]
    print

    print [row['GRBname'] for row in table.iterrows() if 0 <= row['SCounts'] <= 10]

Where args.Scount comes from:

parser.add_argument("--Scount", nargs=2, help="Used to display GRBs within the specified spectral count range")

So for example if I give --Scount 0 and 10:

First line prints ['0', '10'] 0 10 (this was just a test to check args had a value)

Second line prints []

Third line prints a list containing the selected row['GRBname'] within the row['Scount'] condition.

If I switch back to standard loop structure I get the same results, though searching through pytable, argparse and list comprehension documentation hasn't helped me on this specific problem.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Joe McGuire
  • 21
  • 1
  • 3

1 Answers1

3

You are comparing strings with integers; this may work in Python 2.7 but numbers are always considered 'smaller' than strings (sorted before strings).

You also should not compare strings with strings if you expected numerical comparisons. '9' is greater than '10' because 1 sorts before 9 (strings are compared lexicographically):

>>> '1' < 9
False
>>> '9' < '10'
False

Ask argparse for integers:

parser.add_argument(
    "--Scount", nargs=2, type=int,
    help="Used to display GRBs within the specified spectral count range")

and convert row['Scount'] to an integer too if it isn't already:

[row['GRBname'] for row in table.iterrows()
 if args.Scount[0] <= int(row['SCounts']) <= args.Scount[1]]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343