1

我以为我正在理解 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 =线上。

谢谢你的帮助。

4

1 回答 1

5

这里的问题不是你的splitor join,它只是你的列表理解中的一个愚蠢的错误(我们所有人一直犯的那种愚蠢的错误):

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'
于 2013-01-29T02:06:30.430 回答