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?
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