def find_repeats_in_list(lines):
# get lists from every line
all_items = []
for line in lines:
open_bracket = line.index('[')
close_bracket = line.index(']')
items = line[open_bracket+1:close_bracket].split()
all_items.append(items)
# initialize dictionaries to hold consecutive counts
counts = dict()
final = dict()
# seed counts with list from first line
for item in all_items[0]:
counts[item] = 1
# check for first line list items in subsequent lines
for items in all_items[1:]:
for counted in counts:
remove = []
if counted not in items: # not in current line, stop counting
remove.append(counted)
if counts[counted] > 1: # but put in final if more than one
final[counted] = counts[counted]
for item in remove:
del counts[item]
for item in items: # now increment anything consecutive
if item in counts:
counts[item] += 1
return final
最简单的方法是使用re解析文件。在
可以工作的正则表达式:\[([A-Z]\s)+[A-Z]\]
然后用列表“list string”(又名[“[abc]”,“[fgr]”])将其转换为列表。在
对于“[ABC]”“ABC”的格式必须是这样的,因此删除每个空格和[]。在
因此,打印转换后的“字符串”列表将为“ADF”这样的字符串生成类似这样的列表:
^{pr2}$然后合并所有列表并找到重复项。在
这是一个非常前卫的解决方案!我相信有更好的解决办法
当你把重复的东西放进一个列表中时,你可以对它们进行计数:
相关问题 更多 >
编程相关推荐