[python-win32] python-win32 Digest, Vol 116, Issue 13
Tim Roberts
timr at probo.com
Fri Nov 30 22:20:28 CET 2012
In the future, I'd like to suggest that you choose a genuine subject
line when you post. It makes people more inclined to read your messages.
Michael Wilson wrote:
>
> My thought if there's not already an elegant solution in Python is to
> create a flag 'r' before ranges. So, the example here would become
> [1,3,'r',5,10,18,78]. Then I just do a > < query to find a match. How
> does that sound?
I can think of several approaches. For a range, you could add a
two-tuple instead of an integer:
nums = [1, 3, (5,10), 18, 78]
You can tell the difference at runtime:
for item in nums:
if type(item) == tuple:
numHit = (newNum >= item[0]) and (newNum <= item[1])
else:
numHit = newNum == item
To eliminate the special case, you could embed ALL of the numbers as
ranges of size one:
nums = [ (1,1), (3,3), (5,10), (18,18), (78,78) ]
Or, unless you plan to allow numbers in the millions, just add the whole
range to the list individually:
nums = [1, 3]
nums.extend( range(5,10+1) )
nums.append( 18 )
nums.append( 78 )
> for i in range(len(li[x][3])):
Almost always, when you write a for loop with range(len(xxx)), you can
do the same thing without them:
for i in li[x][3]:
If you really do need the index, you can do:
for index, i in enumerate(li[x][3]):
--
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.
More information about the python-win32
mailing list