Reputation: 1036
This is my code:
csvFile = [a,b,c,d,e,...]
brandList = [a,c,e,...]
copyFile = csvFile
for i in csvFile:
for j in List2:
if ' '+j.lower()+' ' in ' '+i.lower()+' ':
print j.lower(), ' ',i.lower()
copyFile.remove(i)
However, after removing an item, the process skips one item. So in a list of [a,b,c,d,e] removing c would skip d entirely (also the print). Be aware that I am not removing from the list I am using to loop. I also tried break. If you remove the "removal-line", print gives me the correct output.
Upvotes: 0
Views: 100
Reputation: 76256
Yes, you are removing from the list you are using to loop. Because copyFile
and csvFile
point to the same list object. Python has purely referential semantics, so assignment makes the new variable point to the same object as the right hand expression, not a deep copy.
If you want to make a copy, construct a new list:
copyFile = list(csvFile)
Upvotes: 3
Reputation: 142136
What it looks like is you can use a regex with multiple conditions, and word boundaries so it only finds whole words, then use that as part of a filter
to recreate a new matching list:
import re
base_items = ['a', 'b', 'c', 'the quick letter a jumped over the dog', 'eeeeee I no match...']
look_for = ['a', 'c', 'e']
rx = re.compile(r'\b({})\b'.format('|'.join(re.escape(item) for item in sorted(look_for, key=len, reverse=True))))
res = filter(rx.search, base_items)
# ['a', 'c', 'the quick letter a jumped over the dog']
Upvotes: 1