conclusion
-
oj 1 重复行的题好玩
-
join : join() 方法用于将
序列
中的元素以指定的字符连接生成一个新的字符串。str.join(sequence)
-
ls.remove()可以去掉某一个元素(一次),如果该行是独特行,去掉该元素后将不在集合t中出现。
-
file_method
file
file.read()
file.readlines()
#coding:utf-8
f=open('D:/workspace/python/source/data.csv')
print(f)
print("-------------")
print(f.read())#read base on char & point at the end
f.seek(0)
print("-------------")
for char in f.read(5):
print(char)
f.seek(0)#put point to the beginning
print("-------------")
lines=f.readlines()#read base on column
# line = line.strip("\n")
# ls = line.split(",")
print(lines)
print("-------------")
for line in lines:
line=line[::-1]
line=line.replace('\n','')
print(line)
f.seek(0)
print("-------------")
for line in f:
#in essence realize the method file.readlines()
print(line)#with \n
line = line.strip("\n")#drop out \n
print(line)
ls = line.split(",")
#divide item by ',' and print with type list
print(ls)
ls = ls[::-1]
print(",".join(ls)) #print with correct format(str)
f.close()
<_io.TextIOWrapper name='D:/workspace/python/source/data.csv' mode='r' encoding='cp936'>
-------------
1,2,3,4,5,6,7
8, 3, 2, 7, 1, 4, 6, 5
6, 1, 3, 8, 5, 7, 4, 2
'a','b','c','x','y','z','i','j','k'
'k', 'b', 'j', 'c', 'i', 'y', 'z', 'a', 'x'
'z', 'c', 'b', 'a', 'k', 'i', 'j', 'y', 'x'
'a', 'y', 'b', 'x', 'z', 'c', 'i', 'j', 'k'
5, 2, 4, 7, 1, 6, 8, 3
-------------
1
,
2
,
3
-------------
['1,2,3,4,5,6,7\n', '8, 3, 2, 7, 1, 4, 6, 5\n', '6, 1, 3, 8, 5, 7, 4, 2\n', "'a','b','c','x','y','z','i','j','k'\n", "'k', 'b', 'j', 'c', 'i', 'y', 'z', 'a', 'x'\n", "'z', 'c', 'b', 'a', 'k', 'i', 'j', 'y', 'x'\n", "'a', 'y', 'b', 'x', 'z', 'c', 'i', 'j', 'k'\n", '5, 2, 4, 7, 1, 6, 8, 3']
-------------
7,6,5,4,3,2,1
5 ,6 ,4 ,1 ,7 ,2 ,3 ,8
2 ,4 ,7 ,5 ,8 ,3 ,1 ,6
'k','j','i','z','y','x','c','b','a'
'x' ,'a' ,'z' ,'y' ,'i' ,'c' ,'j' ,'b' ,'k'
'x' ,'y' ,'j' ,'i' ,'k' ,'a' ,'b' ,'c' ,'z'
'k' ,'j' ,'i' ,'c' ,'z' ,'x' ,'b' ,'y' ,'a'
3 ,8 ,6 ,1 ,7 ,4 ,2 ,5
-------------
1,2,3,4,5,6,7
1,2,3,4,5,6,7
['1', '2', '3', '4', '5', '6', '7']
7,6,5,4,3,2,1
8, 3, 2, 7, 1, 4, 6, 5
8, 3, 2, 7, 1, 4, 6, 5
['8', ' 3', ' 2', ' 7', ' 1', ' 4', ' 6', ' 5']
5, 6, 4, 1, 7, 2, 3,8
6, 1, 3, 8, 5, 7, 4, 2
6, 1, 3, 8, 5, 7, 4, 2
['6', ' 1', ' 3', ' 8', ' 5', ' 7', ' 4', ' 2']
2, 4, 7, 5, 8, 3, 1,6
'a','b','c','x','y','z','i','j','k'
'a','b','c','x','y','z','i','j','k'
["'a'", "'b'", "'c'", "'x'", "'y'", "'z'", "'i'", "'j'", "'k'"]
'k','j','i','z','y','x','c','b','a'
'k', 'b', 'j', 'c', 'i', 'y', 'z', 'a', 'x'
'k', 'b', 'j', 'c', 'i', 'y', 'z', 'a', 'x'
["'k'", " 'b'", " 'j'", " 'c'", " 'i'", " 'y'", " 'z'", " 'a'", " 'x'"]
'x', 'a', 'z', 'y', 'i', 'c', 'j', 'b','k'
'z', 'c', 'b', 'a', 'k', 'i', 'j', 'y', 'x'
'z', 'c', 'b', 'a', 'k', 'i', 'j', 'y', 'x'
["'z'", " 'c'", " 'b'", " 'a'", " 'k'", " 'i'", " 'j'", " 'y'", " 'x'"]
'x', 'y', 'j', 'i', 'k', 'a', 'b', 'c','z'
'a', 'y', 'b', 'x', 'z', 'c', 'i', 'j', 'k'
'a', 'y', 'b', 'x', 'z', 'c', 'i', 'j', 'k'
["'a'", " 'y'", " 'b'", " 'x'", " 'z'", " 'c'", " 'i'", " 'j'", " 'k'"]
'k', 'j', 'i', 'c', 'z', 'x', 'b', 'y','a'
5, 2, 4, 7, 1, 6, 8, 3
5, 2, 4, 7, 1, 6, 8, 3
['5', ' 2', ' 4', ' 7', ' 1', ' 6', ' 8', ' 3']
3, 8, 6, 1, 7, 4, 2,5
OJ practice example
main idea : 重复行(多行)和独特行(单行)对 去重后行数 贡献度 均为 1
删除独特行后仅剩重复行贡献度,贡献度差值即为number of unique
set()=3 after remove set()=1 => **unique =3-1=2**
333
333 ----> 333 => 333 333
333
2 ----> 2 null
1 ----> 1 null
#coding:utf-8
#统计附件文件中与其他任何其他行都不同的行的数量,
#即独特行的数量。
f=open("D:/workspace/python/source/123.txt")
txt=f.readlines()
a=set(txt)#no repetition text
#! but not unique line set
for i in a:
txt.remove(i)
#ls.remove()可以去掉某一个元素,如果该行是独特行,去掉该元素后将不在集合t中出现。
clear=set(txt)
print("去重后行数(非独特行){}行".format(len(a)))
print("共{}独特行".format(len(a)-len(clear)))
去重后行数(非独特行)18行
共12独特行
#coding:utf-8
#统计附件文件中与其他任何其他行都不同的行的数量,
#即独特行的数量。
f=open("D:/workspace/python/source/123.txt")
txt=f.readlines()
a=set(txt)#no repetition text
#! but not unique line set
for i in a:
txt.remove(i)
#ls.remove()可以去掉某一个元素,如果该行是独特行,去掉该元素后将不在集合t中出现。
clear=set(txt)
print("共{}独特行".format(len(a)-len(clear)))
#coding:utf-8
f=open("C:/Users/UMR/Desktop/123.txt","rt")
lines=f.readlines()#test find read with the space ('\n')
lines.reverse()
#print(f)
#print(lines)
for line in lines:
line=line.replace('\n','')
#print(line)
line=line.split(',')
line=line[::-1]
print(';'.join(line))
<_io.TextIOWrapper name='C:/Users/UMR/Desktop/123.txt' mode='rt' encoding='cp936'>
['7,8,9\n', '4,5,6\n', '1,2,3\n']
7,8,9
9;8;7
4,5,6
6;5;4
1,2,3
3;2;1
--------------------------------
9;8;7
6;5;4
3;2;1
#coding:utf-8
f1=open("C:/Users/UMR/Desktop/123.txt","rt")
row=0
col=0
lines1=f1.readlines()#print in type list
print(lines1)
f1.seek(0)
for line in f1:#print in type str
print(line)
if len(line) != 1 and line[-1] =='\n':#value+\n at least 2
row+=1
col+=len(line)-1#discard the '\n'
print(int(col/row+0.5))#rounding off (四舍五入)
['1,2,3\n', '4,5,6\n', '7,8,9\n']
1,2,3
4,5,6
7,8,9
5