[Tutor] Unexpected results using enumerate() and .split()

boB Stepp robertvstepp at gmail.com
Tue Mar 31 22:37:34 CEST 2015


On Tue, Mar 31, 2015 at 3:28 PM, Zachary Ware
<zachary.ware+pytut at gmail.com> wrote:
> On Tue, Mar 31, 2015 at 3:23 PM, boB Stepp <robertvstepp at gmail.com> wrote:
>> The following behavior has me stumped:
>>
>> Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit
>> (Intel)] on win32
>> Type "copyright", "credits" or "license()" for more information.
>>>>> L = ['#ROI:roi_0', '#TXT:text_0', '#1:one^two^three']
>>>>> for i, item in enumerate(L):
>>         subitems = item.split(':')
>>         if subitems[0] == '#ROI':
>>                 print subitems[1]
>>         if subitems[0] == '#TXT':
>>                 print subitems[1]
>>         if subitems[0] == '#1' or '#2':
>
> Here's your problem:  "#2" is always true.  Try "if subitems[0] in
> ['#1', '#2']:"

Thanks, Zach! About the time your reply arrived I was starting to
realize that my '#1' or '#2' might not be doing what I thought. In the
"Python Pocket Reference" I was just looking at:

X or Y     If X is false then Y; else X.

I forgot that different languages have different interpretations of "or".



-- 
boB


More information about the Tutor mailing list