Shibu
Shibu

Reputation: 1542

find all words in list/file that begin/ends with a specific prefix/suffix

The below code gives the words which begin/ends with a specific prefix/suffix:

string_list = [line.strip() for line in open("file.txt", 'r')]
for word in string_list:
    if word[-1] == "a":
        print word

        
string_list = [line.strip() for line in open("file.txt", 'r')]
for word in string_list:
    if word[0] == "fi":
        print word

How can I optimize it to be real fast on huge data?

Upvotes: 1

Views: 3355

Answers (2)

Cadu
Cadu

Reputation: 195

If you need speed, you could simply use GREP, which is written in a lowlevel language and is bound to be faster than a python loop by leaps and bounds.

It is also portable and runs just fine on Linux/Windows/OSX/...

Upvotes: 0

Moses Koledoye
Moses Koledoye

Reputation: 78562

If word is a string, then word[0] == "fi" does not do what you think it does.

You can instead use startswith and endswith to check for multicharacter suffixes and prefixes.

string_list = open("file.txt", 'r')

for word in string_list:
    if word.startswith("fi") or word.endswith('a'):
        print word

To pass the suffix/ prefix as a parameter to your script, have a look at argparse

Upvotes: 2

Related Questions