学习Python Splits和Joins?

时间:2013-01-29 02:03:05

标签: python join python-2.7 split list-comprehension

我以为我理解了python中的分裂和连接,但它不适合我。

说出inp[17] = 'Potters Portland Oregon school of magic'

的值
# a string of data I am pulling from a csv file, the data comes through just fine.
loc = inp[17] 

l = loc.split(' ') # I want to split by the space

# I want to filter out all these words say they don't always 
# come as "School of magic" so I cant just filter that out they 
# could be mixed around at times.

locfilter = ['Potters', 'School', 'of', 'magic']     
locname = ' '.join([value for value in l if l not in locfilter])

此时我的locname变量应该只包含Portland Oregon,但它仍然有'Potters Portland Oregon school of magic',但它没有过滤掉。

我做错了什么我认为问题出在locname =行。

感谢您的帮助。

1 个答案:

答案 0 :(得分:5)

这里的问题不是你的splitjoin,这只是你列表理解条件中的一个愚蠢的错误(我们所有人一直都会犯这种愚蠢的错误):< / p>

locname = ' '.join([value for value in l if l not in locfilter])

显然l永远不在locfilter。如果你解决了这个问题:

locname = ' '.join([value for value in l if value not in locfilter])

工作正常:

'Portland Oregon school'

请注意'school'仍然是输出的一部分。那是因为'school'不在locfilter; 'School'是。如果你想匹配这些不区分大小写:

lowerfilter = [value.lower() for value in locfilter]
locname = ' '.join([value for value in l if value.lower() not in lowerfilter])

现在:

'Portland Oregon'