Created
July 10, 2016 05:40
-
-
Save nametake/d31bc5485cc01b50640461e8664dd28b to your computer and use it in GitHub Desktop.
Get match index list from list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
class MatchIndexGetter(object): | |
def __init__(self, list): | |
self._list = list | |
self._input_string = '' | |
def add_char(self, char): | |
self._input_string += char | |
return self.get_match_count() | |
def clear(self): | |
self._input_string = '' | |
def _search(self): | |
indexes = [] | |
values = [] | |
for i, v in enumerate(self._list): | |
find_index = str(v).find(self._input_string) | |
if find_index == 0: | |
indexes.append(i) | |
values.append(v) | |
return indexes, values | |
def get_match_count(self): | |
indexes, values = self._search() | |
return len(indexes) | |
def get_match_list(self): | |
indexes, values = self._search() | |
return values | |
def get_match_index(self): | |
indexes, values = self._search() | |
return indexes | |
if __name__ == '__main__': | |
hoge = ['hoge', 'fuga', 'piyo', 'hoho', 'pipi'] | |
mig = MatchIndexGetter(hoge) | |
while True: | |
print "=== Start ===" | |
while mig.add_char(raw_input()) != 1: | |
print mig.get_match_count() | |
if mig.get_match_count() == 0: | |
print 'Not found' | |
break | |
print "inputed : {}".format(mig._input_string) | |
print "indexes: {}".format(mig.get_match_list()) | |
print "values : {}".format(mig.get_match_index()) | |
print "=== End ===" | |
print "indexes: {}".format(mig.get_match_list()) | |
print "values : {}".format(mig.get_match_index()) | |
print "" | |
mig.clear() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment