关于文本编辑
1.已知一个记事本里全是数字,,,,无序数字形式(01 02 03 04 05 11 15 18 19 20 08 09)2.如何导入记事本,查数字的重复次数??? >>> f=open("test2.txt")
>>> l=list(f)
>>> l
['01 02 03 04 05 11 15 18 19 20 08 09 01 02 03 04 05 11 15 18 19 20 08 09 01 02 03 04 05 11 15 18 19 20 08 09']
>>> t=l.split()
>>> t
['01', '02', '03', '04', '05', '11', '15', '18', '19', '20', '08', '09', '01', '02', '03', '04', '05', '11', '15', '18', '19', '20', '08', '09', '01', '02', '03', '04', '05', '11', '15', '18', '19', '20', '08', '09']
>>> t2=set(t)
>>> t2
{'11', '08', '03', '04', '15', '19', '01', '18', '20', '09', '05', '02'}
>>> for x in t2:
... print(t.count(x))
...
3
3
3
3
3
3
3
3
3
3
3
3
>>> 本帖最后由 jackz007 于 2022-11-1 06:04 编辑
f , d = open("data.txt") , []
for x in f : d . extend(x . strip() . split())
for x in set(d) :
if d . count(x) > 1 :
print("%s : %d" % (x , d . count(x)))
f . close()
页:
[1]