注册 登录
编程论坛 Python论坛

[求助]分类问题的修改

阿智兄 发布于 2021-03-13 00:04, 1563 次点击
非常感谢你在百忙之中抽空为我解答。希望能在不改变原程序功能的前提下得到你的帮助。
程序代码:
def check(n):
    if len(n)==1:
        return('0')
    else:
        return(n[0])

def output(filename,content):
    f=open(filename,'w+')
    for i in content:
        f.write(' '.join(i)+'\n')
    f.close()
   
   
f=open('data.txt','r+')
data=[i.strip().split('  ') for i in f]
f.close()

index=[tuple(map(lambda x:check(x),i)) for i in data]
indexdict=dict([[i,[]] for i in set(index)])
for i,j in zip(data,index):
    indexdict[j].append(i)
for i in indexdict:
    output(''.join(i)+'.txt',indexdict[i])

上面这个程序作用是对数据做分类,我想将子程序def check(n)改为对数据做奇偶,合单双以及质合的分类,却出错了,以下是我改的子程序:
程序代码:

def check(n): #奇偶判断
    if n % 2 == 0:
        return 0
    else:
        return 1

def check(n):#合单双
    if n == 1:
        return 0
    count = 0
    for c in range(1,n):
        if n % c == 0 :
            count += 1
        if count > 1:
            return 0
    if count == 1:
        return 1

def check(n):#质合数判断
     for i in range(2, n):
         if num % i == 0:
             return False
     return True

 new_list = list(filter(check,range(2,21)))


以下是data.txt的部分数据:
 1  2  3  4  5  13
 1  2  3  4  5  14
 1  2  3  4  5  15
 1  2  3  4  5  16
 1  2  3  4  5  17
 1  2  3  4  5  18
 1  2  3  4  5  19
 1  2  3  4  5  20
 1  2  3  4  5  21
 1  2  3  4  5  22
 1  2  3  4  5  23
 1  6  22  24  27  30
 1  6  22  24  27  31
 1  6  22  24  27  32
 1  6  22  24  27  33
 1  6  22  24  28  29
 1  6  22  24  28  30
 1  6  22  24  28  31
 1  6  22  24  28  32
 2  5  9  10  15  27
 2  5  9  10  15  28
 2  5  9  10  15  29
 2  5  9  10  15  30
 2  5  9  10  15  31
 2  5  9  10  15  32
 3  10  17  19  29  33
 3  10  17  19  30  31
 3  10  17  19  30  32
 3  10  17  19  30  33
 3  10  17  19  31  32
 5  14  20  23  27  33
 5  14  20  23  28  29
 5  14  20  23  28  30
 5  14  20  23  28  31
 12  13  14  16  20  22
 12  13  14  16  20  23
 12  13  14  16  20  24
 12  13  14  16  20  25
 12  13  14  16  20  26
 17  18  22  24  27  30
 17  18  22  24  27  31
 17  18  22  24  27  32
 17  18  22  24  27  33
 17  18  22  24  28  29
4 回复
#2
fall_bernana2021-03-15 11:34
回复 楼主 阿智兄
不太明白想做什么。是这样吗?
程序代码:
def check(n): #奇偶判断
    if int(n) % 2 == 0:
        return '0'
    else:
        return '1'

def output(filename,content):
    print(filename,content,'\n')
   
   
   
f=['1  2  3  4  5  13',
'1  2  3  4  5  14',
'1  2  3  4  5  15',
'1  2  3  4  5  16',
'1  2  3  4  5  17',
'1  2  3  4  5  18',
'1  2  3  4  5  19',
'1  2  3  4  5  20',
'1  2  3  4  5  21',
'1  2  3  4  5  22',
'1  2  3  4  5  23',
'1  6  22  24  27  30',
'1  6  22  24  27  31',
'1  6  22  24  27  32',
'1  6  22  24  27  33',
'1  6  22  24  28  29',
'1  6  22  24  28  30',
'1  6  22  24  28  31',
'1  6  22  24  28  32',
'2  5  9  10  15  27',
'2  5  9  10  15  28',
'2  5  9  10  15  29',
'2  5  9  10  15  30',
'2  5  9  10  15  31',
'2  5  9  10  15  32',
'3  10  17  19  29  33',
'3  10  17  19  30  31',
'3  10  17  19  30  32',
'3  10  17  19  30  33',
'3  10  17  19  31  32',
'5  14  20  23  27  33',
'5  14  20  23  28  29',
'5  14  20  23  28  30',
'5  14  20  23  28  31',
'12  13  14  16  20  22',
'12  13  14  16  20  23',
'12  13  14  16  20  24',
'12  13  14  16  20  25',
'12  13  14  16  20  26',
'17  18  22  24  27  30',
'17  18  22  24  27  31',
'17  18  22  24  27  32',
'17  18  22  24  27  33',
'17  18  22  24  28  29']
data=[i.strip().split('  ') for i in f]


index=[tuple(map(lambda x:check(x),i)) for i in data]


indexdict=dict([[i,[]] for i in set(index)])

for i,j in zip(data,index):
    indexdict[j].append(i)

for i in indexdict:
    output(''.join(i)+'.txt',indexdict[i])
#3
阿智兄2021-03-15 20:15
回复 2楼 fall_bernana
非常感谢你的回复。
问题解决了。

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

#4
sssooosss2021-03-18 08:34
共同学习
#5
foxprosue2021-03-19 14:57
学习
1