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

boB Stepp robertvstepp at gmail.com
Tue Mar 31 22:23:55 CEST 2015


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':
                print subitems[1]

roi_0
roi_0
text_0
text_0
one^two^three
>>>

My desired output was:

roi_0
text_0
one^two^three

Oh, wonderful founts of wisdom, where is my understanding lacking?

BTW, I copied and pasted the above into my Gmail window, but it
removed the indentation that was present in the interpreter. I added
spaces manually to get it appear as it did in the interpreter. Anyone
know why Gmail does that to my copy and paste?

-- 
boB


More information about the Tutor mailing list