Wow. What an amazingly helpful list. Thanks! Apologies for not fixing the subject. I was actually kicking myself right after I hit send for not personalizing that. I really like the options Tim has given me. I suppose I will try them out and see which one is most efficient for my application. Right now, I'm leaning towards the nums = [1, 3, (5,10), 18, 78] option.
This is what I will be trying out: nums = [1, 3, (5,10), 18, 78] for item in nums: if isinstance(item,tuple): #Thanks Paul numHit = (newNum >= item[0]) and (newNum <= item[1]) else: numHit = newNum == item -Michael On Sat, Dec 1, 2012 at 5:00 AM, <python-win32-requ...@python.org> wrote: > Send python-win32 mailing list submissions to > python-win32@python.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://mail.python.org/mailman/listinfo/python-win32 > or, via email, send a message with subject or body 'help' to > python-win32-requ...@python.org > > You can reach the person managing the list at > python-win32-ow...@python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of python-win32 digest..." > > > Today's Topics: > > 1. Re: python-win32 Digest, Vol 116, Issue 13 (Michael Wilson) > 2. Re: python-win32 Digest, Vol 116, Issue 13 (Tim Roberts) > 3. Re: python-win32 Digest, Vol 116, Issue 13 (paul_kon...@dell.com) > 4. Re: python-win32 Digest, Vol 116, Issue 13 (Tim Roberts) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Fri, 30 Nov 2012 14:12:02 -0600 > From: Michael Wilson <mich...@siteshadow.com> > To: python-win32@python.org > Subject: Re: [python-win32] python-win32 Digest, Vol 116, Issue 13 > Message-ID: > <CAKXVczE= > pww0qhcv_y3g7jne7kq_n_kfticn9brv++mc8yp...@mail.gmail.com> > Content-Type: text/plain; charset="iso-8859-1" > > Hi everyone, > > I am new to Python. Been poking at it for about a month now. I am trying to > set up a list for information on people. > For example: > person 0's name = "Sam". li[0][0] > person 0's hoppy = "skateboarding" li[0][1] > person 0's favorite color = ["green", "red"] li[0][2] # it appears i can > put another list inside of the list, ie li[0][2]=[x,y,z] very cool! we > couldn't do that in my grandpa languages. Would that be done with a > li[0].append(['green','red']) after I'm done defining li[0][1]?? Then, I > could just do a li[0][2].extend(['purple','mauve']) if more colors were > ever needed? > person 0's favorite numbers = [1, 3, 5-10, 78] li[0][3] # ranges of > numbers? > person 1's name = "Mary" li[1][0] > etc.. > > 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? > newNum = 8 > numHit = False > for i in range(len(li[x][3])): > if li[x][3][i] == 'r': > if li[x][3][i+1] < newNum > li[x][3][i+2]: # I didn't do <= and >= > because the = parts will be evaluated in the next 2 iterations of the loop > numHit == True > else: > if newNum == li[x][3][i]: > numHit == True > if numHit: > print "You picked one of " + li[x][0] + "'s lucky numbers!" > > How does that look? Is that ~ the best way to address my problem? > > Thanks! > Michael > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: < > http://mail.python.org/pipermail/python-win32/attachments/20121130/2654d10b/attachment-0001.html > > > > ------------------------------ > > Message: 2 > Date: Fri, 30 Nov 2012 13:20:28 -0800 > From: Tim Roberts <t...@probo.com> > To: Python-Win32 List <python-win32@python.org> > Subject: Re: [python-win32] python-win32 Digest, Vol 116, Issue 13 > Message-ID: <50b9231c.8090...@probo.com> > Content-Type: text/plain; charset="ISO-8859-1" > > 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, t...@probo.com > Providenza & Boekelheide, Inc. > > > > ------------------------------ > > Message: 3 > Date: Fri, 30 Nov 2012 21:23:56 +0000 > From: <paul_kon...@dell.com> > To: <t...@probo.com> > Cc: python-win32@python.org > Subject: Re: [python-win32] python-win32 Digest, Vol 116, Issue 13 > Message-ID: > <c75a84166056c94f84d238a44af9f6ad2be...@ausx10mpc103.amer.dell.com > > > Content-Type: text/plain; charset="us-ascii" > > > On Nov 30, 2012, at 4:20 PM, Tim Roberts wrote: > > > 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: > > Nit: the Pythonic recommended way of doing this is "if isinstance (item, > tuple):" > > The difference is that this will also work if item is an instance of a > subclass of "tuple" while the original code does not handle that case. For > this code, that might not be an interesting scenario, but in other places > it might be. It's worth getting into the habit of using "isinstance". > > paul > > > > > ------------------------------ > > Message: 4 > Date: Fri, 30 Nov 2012 13:27:24 -0800 > From: Tim Roberts <t...@probo.com> > To: Python-Win32 List <python-win32@python.org> > Subject: Re: [python-win32] python-win32 Digest, Vol 116, Issue 13 > Message-ID: <50b924bc.1010...@probo.com> > Content-Type: text/plain; charset="ISO-8859-1" > > paul_kon...@dell.com wrote: > > > > Nit: the Pythonic recommended way of doing this is "if isinstance (item, > tuple):" > > > > The difference is that this will also work if item is an instance of a > subclass of "tuple" while the original code does not handle that case. For > this code, that might not be an interesting scenario, but in other places > it might be. It's worth getting into the habit of using "isinstance". > > Well said. I have several bad Python habits that I should work on > eliminating... > > -- > Tim Roberts, t...@probo.com > Providenza & Boekelheide, Inc. > > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > python-win32 mailing list > python-win32@python.org > http://mail.python.org/mailman/listinfo/python-win32 > > > ------------------------------ > > End of python-win32 Digest, Vol 117, Issue 1 > ******************************************** >
_______________________________________________ python-win32 mailing list python-win32@python.org http://mail.python.org/mailman/listinfo/python-win32