我有一个字符串列表 mylist = [“3> 3> 4> 5”,“2> 2> 4”,“3> 3> 5> 6> 2”,“2> 2> 4> 5”,“4> 5> 5” ]
我希望能够在此列表中找到子模式
例如,通过模式查找器后的最终结果应返回
{“3> 3>”:[“3> 3> 4> 5”,3> 3> 5> 6> 2],“2> 2> 4”:[“2> 2> 4”, “2→2→4→5”]}
目前,我可以按字符串中的第一个字母对列表进行分组。 通过find_sub_pattern解析列表mylist导致[[“3> 3> 4> 5”,3> 3> 5> 6> 2],[“2> 2> 4”,“2> 2> 4> 5]] < / p>
def find_sub_pattern(data=[]):
all_match=[]
first_letter=[]
for row in data:
first_letter.append(row[0])
list_freq=get_list_freq(first_letter)
matched_first=[]
for key, value in list_freq.items():
if value > 1:
matched_first.append(key)
if matched_first==[]:
return "No pattern match"
matched_array=[]
for p in range(0,len(matched_first)):
matched_array.append([x for x in data if x[0] in matched_first[p]])
print(matched_array)
答案 0 :(得分:1)
这样做你想要的:
def common_start(sa, sb):
def _iter():
for a, b in zip(sa, sb):
if a != b:
return
yield a
return list(_iter())
l = ["3>3>4>5","2>2>4","3>3>5>6>2","2>2>4>5", "4>5>5"]
elems = [x.split(">") for x in l]
groups = [[x for x in elems if x[0] == group] for group in {x[0] for x in elems}]
result = {
">".join(reduce(common_start, group)):
[">".join(x) for x in group] for group in groups if 1 < len(group)
}