python 匹配字符串多个_在Python中匹配多个数据集的字符串
這里有一小段你可以啟發(fā)的代碼。主要思想是使用遞歸函數(shù)。
為簡單起見,我承認(rèn)我已經(jīng)在列表中加載了數(shù)據(jù),但是你可以在之前從文件中獲取它們:
data_files = [
'data_a.dat',
'data_b.dat',
'data_c.dat',
'data_d.dat',
'data_e.dat',
]
lists = [pickle.load(open(f)) for f in data_files]而且因?yàn)椴]有真正了解你真正需要做的事情,我的目標(biāo)是在四個(gè)第一個(gè)字符上找到匹配:
def do_wathever(string):
print "I have match the string '%s'" % string
lists = [
["hello", "world", "how", "grown", "you", "today", "?"],
["growl", "is", "a", "now", "on", "appstore", "too bad"],
["I", "wish", "I", "grow", "Magnum", "mustache", "don't you?"],
]
positions = [0 for i in range(len(lists))]
def recursive_match(positions, lists):
strings = map(lambda p, l: l[p], positions, lists)
match = True
searched_string = strings.pop(0)[:4]
for string in strings:
if searched_string not in string:
match = False
break
if match:
do_wathever(searched_string)
# increment positions:
new_positions = positions[:]
lists_len = len(lists)
for i, l in enumerate(reversed(lists)):
max_position = len(l)-1
list_index = lists_len - i - 1
current_position = positions[list_index]
if max_position > current_position:
new_positions[list_index] += 1
break
else:
new_positions[list_index] = 0
continue
return new_positions, not any(new_positions)
search_is_finished = False
while not search_is_finished:
positions, search_is_finished = recursive_match(positions, lists)當(dāng)然你可以在這里優(yōu)化很多東西,這是草稿代碼,但是看看recursive function,這是一個(gè)主要的概念。
總結(jié)
以上是生活随笔為你收集整理的python 匹配字符串多个_在Python中匹配多个数据集的字符串的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql varchar(max)_等
- 下一篇: python兼容性怎么样_Python与