![]() |
#2
fall_bernana2021-03-15 11:34
回复 楼主 阿智兄
|
![](zzz/editor/img/code.gif)
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])
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)改为对数据做奇偶,合单双以及质合的分类,却出错了,以下是我改的子程序:
![](zzz/editor/img/code.gif)
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