- import re
- import os,sys
- list=['000000000000001101111111',
- '111111000000000010011111',
- '101111111111111111110000',
- '001111111001111111111111',
- '111111111111111111110011']
- i=1#计数器
- #把数据保存在本地
- #print(os.path.dirname(os.path.realpath(__file__)))
- f = open(os.path.dirname(os.path.realpath(__file__))+"\counts.txt",'a')
- f.seek(0)
- f.truncate()#清空文件
- l_times_0=[]#0出现次数
- l_longest_0=[]#最长0的个数
- l_longest_1=[]#最长1的个数
- for num_str in list:
- print("第%d个字符串"%i)
- #第一步求有多少次0出现,存在变量times_0里面
- re_0 = "0{1,}"
- pattern0 = re.compile(re_0)
- match_0 = pattern0.findall(num_str)#用正则找到所有的0
- times_0 =len(match_0)#2
-
- #第二步求0的最大连续值,存在变量longest_0里面
- counts_0 = [len(i) for i in match_0]#用列表生成式求每次的0出现了多少个
- longest_0 = max(counts_0)#14
-
- #第三步求1的最大连续值,存在变量longest_1里面
- re_1 = "1{1,}"
- pattern1 = re.compile(re_1)
- match_1 = pattern1.findall(num_str)#用正则找到所有的1
- counts_1 = [len(i) for i in match_1]#用列表生成式求每次的1出现了多少个
- longest_1 = max(counts_1)#7
- i+=1
-
- l_times_0.append(times_0)#0出现次数
- l_longest_0.append(longest_0)#最长0的个数
- l_longest_1.append(longest_1)#最长1的个数
- f.write("%d,%d,%d\n"%(times_0,longest_0,longest_1))
- f.close()



雷达卡




京公网安备 11010802022788号







