I thought I was understanding splits and joins in python but it is not working right for me.
lets say the value of 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])
At this point my locname variable should only have Portland Oregon in it, but it still has 'Potters Portland Oregon school of magic' it didn’t filter out.
What have I done wrong I think the problem is in my locname = line.
Thanks for any help.
The problem here isn’t your
splitorjoin, it’s just a silly mistake in the condition in your list comprehension (the kind of silly mistake all of us make all the time):Obviously
lis never inlocfilter. And if you fix that:It works fine:
Note that
'school'is still part of the output. That’s because'school'wasn’t inlocfilter;'School'was. If you want to match these case-insensitively:And now: