标题:[求助]有关连号分类问题
只看楼主
阿智兄
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-3-8
结帖率:87.5%
已结贴  问题点数:20 回复次数:5 
[求助]有关连号分类问题
数据文本(data.txt)中有N行数据,现要将冒号后面的数据进行分类,分类规则如下:
无连号:01,03,05,07,09,21  这种类型分为0类
单二连号:01,02,05,11,21,24 这种类型分为单二类
双二连号:01,02,14,15,17,22 这种类型分为双二类
三二连号:01,02,13,14,27,28 这种类型分为三二类
单三连号:01,02,03,11,15,17 这种类型分为单三类
双三连号:01,02,03,10,11,12 这种类型分为双三类
二连号+三连号:01,02,15,20,21,22 这种类型分为二+三类
二连号+四连号:01,02,18,19,20,21 这种类型分为二+四类
三连号+二连号:01,02,03,21,23,28,29 这种类型分为三+二类
四连号+二连号:001,02,03,04,21,22 这种类型分为四+二类
四连号:01,02,03,04,11,16,17 这种类型分为四类
五连号:01,02,03,04,05,17 这种类型分为五类
六连号:01,02,03,04,05,06 这种类型分为六类
每种分类独立用一个文本装。

以下是data.txt部分数据
A10A21A34A44A57A74:01,02,05,16,18,19
A2A11A27A30A45A60:01,02,06,08,12,19
A6A10A16A26A41A60:01,02,07,08,10,11
A1A7A14A35A48A56:03,06,17,18,19,30
A1A7A35A52A53A56:03,06,17,18,30,32
A26A33A52A56A63A74:03,06,17,20,21,22
A2A7A24A56A66A68:03,06,17,20,23,30
A2A5A38A55A63A74:05,12,16,19,21,33
A1A11A17A60A67A74:05,12,16,19,22,27
A2A9A17A46A56A57:05,12,17,19,21,27
A5A6A22A38A56A71:05,12,17,20,21,32
A3A7A20A30A60A63:06,08,10,12,18,23
A2A3A24A35A45A60:06,08,10,12,19,30
A4A27A36A38A47A51:06,08,10,14,17,22
A3A20A44A50A55A74:06,08,10,14,18,30
A2A23A28A30A34A55:06,08,10,15,26,28
A3A4A30A40A55A79:06,08,10,18,19,23
A1A24A26A64A70A77:08,19,20,22,23,33
A2A19A54A66A77A79:08,19,20,23,27,33
A13A18A30A60A62A72:09,10,11,12,18,23
A2A16A29A33A60A75:09,10,11,12,20,26
A5A7A18A58A63A79:14,17,19,20,23,33
A4A5A38A57A75A77:14,18,19,20,21,31
A5A8A9A28A44A57:14,18,19,21,25,27
A4A5A38A68A75A77:14,18,20,21,31,32
A1A5A26A50A60A68:14,20,22,23,30,32
A1A4A19A48A67A77:15,16,19,20,22,33

搜索更多相关主题的帖子: 类型 数据 data 分类 txt 
2021-03-24 23:57
zyb159357
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:25
专家分:113
注 册:2021-3-15
得分:8 
#给你写了一点作为思路(现可判定6连,4连,3连),剩余的自己搞定哈。

def si(lst): #定义4连顺.
    for i in lst[:-1]:
        sub={i,i+1,i+2,i+3}        
        if sub.issubset(lst):
            c=sorted(list(set(lst)-sub))           
            if c==[c[0],c[0]+1]:
                return "4_2" #四连+二连
            return "4_1" #四连+单
    return 0 #非四连.

def liu(lst): #定义6连顺.
    lst.sort()
    if lst==[lst[0],lst[0]+1,lst[0]+2,lst[0]+3,lst[0]+4,lst[0]+5]:
        return '6_6' #6连顺
    return 0   

def san(lst): #定义三连函数
    for i in lst[:-2]:
        sub={i,i+1,i+2}        
        if sub.issubset(lst):
            c=sorted(list(set(lst)-sub))           
            if c==[c[0],c[0]+1,c[0]+2]:
                return "3_3" #三连+三连
            if c[:2]==[c[0],c[0]+1] or c[1:3]==[c[1],c[1]+1]:
                return "3_2"   #三连+二连        
            return "3_1" #三连+单
    return 0 #非三连
   
def writefile(path_file:str ,data):
    with open(path_file,"a") as fw:
        fw.write(data)   
         
def classify(data):
    s=data.split(":")[1].strip()
    lst=list(map(int,s.split(",")))#把列表元素转换成int型.
    f=liu(lst)   
    if f: #如果6顺,写入对应文件(6_6.txt)
        writefile(f+".txt",data)
        return
    f=si(lst)
    if f: #如果是4连,写入对应文件(4_x.txt)
        writefile(f+".txt",data)
        return   
    f=san(lst)
    if f: #如果是3连,写入对应文件(3_x.txt)
        writefile(f+".txt",data)
        return

if __name__ == "__main__":           
    for data in open("./data.txt"):
        classify(data)
2021-03-25 15:06
fall_bernana
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:17
帖 子:240
专家分:2086
注 册:2019-8-16
得分:12 
回复 楼主 阿智兄
程序代码:
datas=['A10A21A34A44A57A74:01,02,05,16,18,19',
'A2A11A27A30A45A60:01,02,06,08,12,19',
'A6A10A16A26A41A60:01,02,07,08,10,11',
'A1A7A14A35A48A56:03,06,17,18,19,30',
'A1A7A35A52A53A56:03,06,17,18,30,32',
'A26A33A52A56A63A74:03,06,17,20,21,22',
'A2A7A24A56A66A68:03,06,17,20,23,30',
'A2A5A38A55A63A74:05,12,16,19,21,33',
'A1A11A17A60A67A74:05,12,16,19,22,27',
'A2A9A17A46A56A57:05,12,17,19,21,27',
'A5A6A22A38A56A71:05,12,17,20,21,32',
'A3A7A20A30A60A63:06,08,10,12,18,23',
'A2A3A24A35A45A60:06,08,10,12,19,30',
'A4A27A36A38A47A51:06,08,10,14,17,22',
'A3A20A44A50A55A74:06,08,10,14,18,30',
'A2A23A28A30A34A55:06,08,10,15,26,28',
'A3A4A30A40A55A79:06,08,10,18,19,23',
'A1A24A26A64A70A77:08,19,20,22,23,33',
'A2A19A54A66A77A79:08,19,20,23,27,33',
'A13A18A30A60A62A72:09,10,11,12,18,23',
'A2A16A29A33A60A75:09,10,11,12,20,26',
'A5A7A18A58A63A79:14,17,19,20,23,33',
'A4A5A38A57A75A77:14,18,19,20,21,31',
'A5A8A9A28A44A57:14,18,19,21,25,27',
'A4A5A38A68A75A77:14,18,20,21,31,32',
'A1A5A26A50A60A68:14,20,22,23,30,32',
'A1A4A19A48A67A77:15,16,19,20,22,33',
'A:01,03,05,07,09,21',
'B:01,02,05,11,21,24',
'C:01,02,14,15,17,22',
'D:01,02,13,14,27,28',
'E:01,02,03,11,15,17',
'F:01,02,03,10,11,12',
'G:01,02,15,20,21,22',
'H:01,02,18,19,20,21',
'I:01,02,15,20,21,22',
'J:01,02,03,21,23,28,29',
'K:01,02,03,04,21,22',
'L:01,02,03,04,11,16,17',
'M:01,02,03,04,05,17',
'N:01,02,03,04,05,06',]
import re
filecount={}
for data in datas:
    data_count=re.sub('^.*?:','',data)
    #print(data)
    startnum=0
    count=0
    filename={1:0,2:0,3:0,4:0,5:0,6:0}
    for num in sorted(data_count.split(',')):
        print(num + " ",end='')
        if startnum==0 or int(num)-startnum==1:
            startnum=int(num)
            count+=1
        else:
            filename[count]+=1
            count=1
            startnum=int(num)
    print()
    filename[count]+=1
    fileoutname='___'.join(list(map(lambda y: str(y[0])+"-"+str(y[1]),sorted(filename.items(), key=lambda x : x[0]))))
    print(fileoutname)
    if fileoutname in filecount:
        filecount[fileoutname]=filecount[fileoutname]+1
    else:
        filecount[fileoutname]=1
    with open (fileoutname,"a+") as fileout:
        fileout.write(data+"\n")
for fileoutnamekey in filecount:
    print(fileoutnamekey,filecount[fileoutnamekey])


01 02 05 16 18 19 
1-2___2-2___3-0___4-0___5-0___6-0
01 02 06 08 12 19
1-4___2-1___3-0___4-0___5-0___6-0
01 02 07 08 10 11
1-0___2-3___3-0___4-0___5-0___6-0
03 06 17 18 19 30
1-3___2-0___3-1___4-0___5-0___6-0
03 06 17 18 30 32
1-4___2-1___3-0___4-0___5-0___6-0
03 06 17 20 21 22
1-3___2-0___3-1___4-0___5-0___6-0
03 06 17 20 23 30
1-6___2-0___3-0___4-0___5-0___6-0
05 12 16 19 21 33
1-6___2-0___3-0___4-0___5-0___6-0
05 12 16 19 22 27
1-6___2-0___3-0___4-0___5-0___6-0
05 12 17 19 21 27
1-6___2-0___3-0___4-0___5-0___6-0
05 12 17 20 21 32
1-4___2-1___3-0___4-0___5-0___6-0
06 08 10 12 18 23
1-6___2-0___3-0___4-0___5-0___6-0
06 08 10 12 19 30
1-6___2-0___3-0___4-0___5-0___6-0
06 08 10 14 17 22
1-6___2-0___3-0___4-0___5-0___6-0
06 08 10 14 18 30
1-6___2-0___3-0___4-0___5-0___6-0
06 08 10 15 26 28
1-6___2-0___3-0___4-0___5-0___6-0
06 08 10 18 19 23
1-4___2-1___3-0___4-0___5-0___6-0
08 19 20 22 23 33
1-2___2-2___3-0___4-0___5-0___6-0
08 19 20 23 27 33
1-4___2-1___3-0___4-0___5-0___6-0
09 10 11 12 18 23
1-2___2-0___3-0___4-1___5-0___6-0
09 10 11 12 20 26
1-2___2-0___3-0___4-1___5-0___6-0
14 17 19 20 23 33
1-4___2-1___3-0___4-0___5-0___6-0
14 18 19 20 21 31
1-2___2-0___3-0___4-1___5-0___6-0
14 18 19 21 25 27
1-4___2-1___3-0___4-0___5-0___6-0
14 18 20 21 31 32
1-2___2-2___3-0___4-0___5-0___6-0
14 20 22 23 30 32
1-4___2-1___3-0___4-0___5-0___6-0
15 16 19 20 22 33
1-2___2-2___3-0___4-0___5-0___6-0
01 03 05 07 09 21
1-6___2-0___3-0___4-0___5-0___6-0
01 02 05 11 21 24
1-4___2-1___3-0___4-0___5-0___6-0
01 02 14 15 17 22
1-2___2-2___3-0___4-0___5-0___6-0
01 02 13 14 27 28
1-0___2-3___3-0___4-0___5-0___6-0
01 02 03 11 15 17
1-3___2-0___3-1___4-0___5-0___6-0
01 02 03 10 11 12
1-0___2-0___3-2___4-0___5-0___6-0
01 02 15 20 21 22
1-1___2-1___3-1___4-0___5-0___6-0
01 02 18 19 20 21
1-0___2-1___3-0___4-1___5-0___6-0
01 02 15 20 21 22
1-1___2-1___3-1___4-0___5-0___6-0
01 02 03 21 23 28 29
1-2___2-1___3-1___4-0___5-0___6-0
01 02 03 04 21 22
1-0___2-1___3-0___4-1___5-0___6-0
01 02 03 04 11 16 17
1-1___2-1___3-0___4-1___5-0___6-0
01 02 03 04 05 17
1-1___2-0___3-0___4-0___5-1___6-0
01 02 03 04 05 06
1-0___2-0___3-0___4-0___5-0___6-1

1-2___2-2___3-0___4-0___5-0___6-0 5
1-4___2-1___3-0___4-0___5-0___6-0 9
1-0___2-3___3-0___4-0___5-0___6-0 2
1-3___2-0___3-1___4-0___5-0___6-0 3
1-6___2-0___3-0___4-0___5-0___6-0 10
1-2___2-0___3-0___4-1___5-0___6-0 3
1-0___2-0___3-2___4-0___5-0___6-0 1
1-1___2-1___3-1___4-0___5-0___6-0 2
1-0___2-1___3-0___4-1___5-0___6-0 2
1-2___2-1___3-1___4-0___5-0___6-0 1
1-1___2-1___3-0___4-1___5-0___6-0 1
1-1___2-0___3-0___4-0___5-1___6-0 1
1-0___2-0___3-0___4-0___5-0___6-1 1






[此贴子已经被作者于2021-3-25 16:21编辑过]

2021-03-25 16:09
阿智兄
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-3-8
得分:0 
回复 3楼 fall_bernana
谢谢你的帮助。
代码简洁,高效,结果明了,好厉害!
从中学习到了很多,非常感谢。
2021-03-25 17:08
阿智兄
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-3-8
得分:0 
回复 楼主 阿智兄
非常感谢你的帮助,虽然只给了一部分的算法,但是我会根据它继续完善的,并从完善的过程中学习更多的知识。
2021-03-25 17:10
zyb159357
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:25
专家分:113
注 册:2021-3-15
得分:0 
get到了。版主思路确实非凡人, 这里字典使用如有神助.
2021-03-26 00:01



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-505239-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.099528 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved