Fast
Fast

Reputation: 43

What function should I use to filter a text file for any element in array?

I have a list of words that I'm trying to filter from a text file stored in an array, but I'm not sure what function to use. Here's my code:

words = ["liverpool","arsenal","chelsea","manutd","mancity"]
test = ["LiverpoolFan","ArsenalFan"]
test2 = []

for i in range (len(test)):
    test2[i] = test[i].lower()
    if *any word in words* in test2[i]:
        print("True")

I've used a test array to simulate reading from a text file. I'm not sure what to use inbetween the two **

Upvotes: 1

Views: 62

Answers (4)

Sayandip Dutta
Sayandip Dutta

Reputation: 15872

You can use builtin any:

>>> test2 = [team for team in words 
             if any(team.lower() in fanbase.lower() for fanbase in test)]
>>> test2
['liverpool', 'arsenal']

Or any with filter:

>>> def check_match(team):
        return any(team.lower() in fanbase.lower() for fanbase in test)
>>> test2 = list(filter(check_match, words))
>>> test2
['liverpool', 'arsenal']

Or you could use str.join with a separator that is not in your words list, such as ',':

>>> all_fans = ','.join(test).lower()
>>> test2 = [team for team in words if team in all_fans]
>>> test2
['liverpool', 'arsenal']

Upvotes: 1

Umutambyi Gad
Umutambyi Gad

Reputation: 4101

Here is simple attempt, you can simply remove Fan from the test in order to check if there is team in words match that in test

import re

words = ["liverpool","arsenal","chelsea","manutd","mancity"]
test = ["LiverpoolFan","ArsenalFan"]
purified_test = [re.sub('Fan','', i) for i in test]
print(test)
for i in words:
    if i.title() in test:
        print('True');

or make it lower() while removing Fan

purified_test = [re.sub('Fan','', i).lower() for i in test]
for i in words:
    if i in purified_test:
        print('True');

or you can append it to test_2 and get the array like the following

import re
words = ["liverpool","arsenal","chelsea","manutd","mancity"]
test = ["LiverpoolFan","ArsenalFan"]
test2 = []


purified_test = [re.sub('Fan','', i).lower() for i in test]
for i in words:
    if i in purified_test:
        test2.append(i)
print(test2)

output

['liverpool', 'arsenal']

if all files don't all end in fan you can simple make the character set of all words that ends it like the following

import re
regex = re.compile('(Fan|FC|etc)')

Upvotes: 0

Kamil Kozioł
Kamil Kozioł

Reputation: 194

You can do something like this

words = ["liverpool","arsenal","chelsea","manutd","mancity"]
fans = ["LiverpoolFan","ArsenalFan"]

for fan in fans:
  for word in words:
    if word.lower() in fan.lower():
      print(f"{word} in {fan}", True)

Upvotes: 1

Banana
Banana

Reputation: 2533

brute force approach. Checks for every item if any word from words contains it

matches = []
for item in test:
    for word in words:
       # word.lower() thanks to  @sushanth
       if item.lower() in word.lower():
           matches.append(item)
           print("True")

Upvotes: 0

Related Questions