2017-02-23 54 views

回答

0

filter(None,re.split())will do!

>>> #st is the input and res is the list 
>>> st="""'<strong class="linkBlack">College Forum</strong>, <strong class="linkBlack">Intro Info Tech</strong>, <strong class="linkBlack">Earth Science</strong>, <strong class="linkBlack">Sec. Math 1</strong>, <strong class="linkBlack">Astronomy</strong>, <strong class="linkBlack">Computer Tech</strong>, <strong class="linkBlack">Human Geography H</strong>, <strong class="linkBlack">English 9</strong>, <strong class="linkBlack">Sec. Math 1</strong>, <strong class="linkBlack">Chess</strong>, <strong class="linkBlack">College Forum</strong>, <strong class="linkBlack">Intro Info Tech</strong>, <strong class="linkBlack">Earth Science</strong>, <strong class="linkBlack">Sec. Math 1</strong>, <strong class="linkBlack">Astronomy</strong>, <strong class="linkBlack">Computer Tech</strong>, <strong class="linkBlack">Human Geography H</strong>, <strong class="linkBlack">English 9</strong>, <strong class="linkBlack">Sec. Math 1</strong>, <strong class="linkBlack">College Forum</strong>, <strong class="linkBlack">A+ Comp Rep/Maint</strong>, <strong class="linkBlack">Earth Science</strong>, <strong class="linkBlack">Sec. Math 1</strong>, <strong class="linkBlack">Aikido</strong>, <strong class="linkBlack">Exploring Comp Sci</strong>, <strong class="linkBlack">World History H</strong>, <strong class="linkBlack">English 9</strong>, <strong class="linkBlack">Sec. Math 1</strong>, <strong class="linkBlack">College Forum</strong>, <strong class="linkBlack">A+ Comp Rep/Maint</strong>, <strong class="linkBlack">Earth Science</strong>, <strong class="linkBlack">Sec. Math 1</strong>, <strong class="linkBlack">Aikido</strong>, <strong class="linkBlack">Exploring Comp Sci</strong>, <strong class="linkBlack">World History H</strong>, <strong class="linkBlack">English 9</strong>, <strong class="linkBlack">Sec. Math 1</strong>'""" 
>>> #split the string with comma and for each 
>>> #apply the regex filter after stripping the beginning and trailing white space. 
>>> res = [filter(None,re.split('(<strong class="linkBlack">)| 
(<\/strong*>)',s.strip()))[1] for s in st.split(",")] 
['College Forum', 'Intro Info Tech', 'Earth Science', 'Sec. Math 1', 'Astronomy', 'Computer Tech', 'Human Geography H', 'English 9', 'Sec. Math 1', 'Chess', 'College Forum', 'Intro Info Tech', 'Earth Science', 'Sec. Math 1', 'Astronomy', 'Computer Tech', 'Human Geography H', 'English 9', 'Sec. Math 1', 'College Forum', 'A+ Comp Rep/Maint', 'Earth Science', 'Sec. Math 1', 'Aikido', 'Exploring Comp Sci', 'World History H', 'English 9', 'Sec. Math 1', 'College Forum', 'A+ Comp Rep/Maint', 'Earth Science', 'Sec. Math 1', 'Aikido', 'Exploring Comp Sci', 'World History H', 'English 9', 'Sec. Math 1'] 

希望這會有所幫助!

+0

我明白了,不過謝謝:) – PinkChicken