counts = dict()
_str = open("test.txt", encoding="utf-8")
words = _str.read().split()
for word in words :
counts[word] = counts.get(word, 0) + 1
bigWord = {"word":None, "count":None}
for k, v in counts.items() :
if bigWord["word"] is None or bigWord["count"] < v :
bigWord["word"] = k
bigWord["count"] = v
print("The Most common words : ", bigWord)
# 많이 사용한 단어를 우선으로 - 전체 출력
# lambda 이용
rank = sorted(counts.items(), key=lambda item:item[1], reverse=True)
for i in rank :
print("{:>20} : {:5}".format(i[0], i[1]))
print()
print("-" * 70)
print()
# 많이 사용한 단어를 우선으로 - 상위 10개 출력
# lambda 이용
rank = sorted(counts.items(), key=lambda item:item[1], reverse=True)
for k, v in rank[0:10] :
print("{:>20} : {:5}".format(k, v))
print()
print("-" * 70)
print()
# 많이 사용한 단어를 우선으로 - 상위 10개 출력
# List comprehension 이용
rank = sorted([(v, k) for k, v in counts.items()], reverse=True) # sorted함수를 이용하면 tuple의 왼쪽 첫번째항목을 키로 정렬한다. 그래서 key와 value의 위치변경
for v, k in rank[0:10] : # 다시 출력을 위해서 key와 value의 위치변경
print("{:>20} : {:05}".format(k, v))
실행결과
-------------------------------------
>>> exec(open("string2.py", encoding="utf-8").read())
The Most common words : {'word': 'to', 'count': 16}
to : 16
the : 6
and : 5
do : 5
our : 5
computers : 5
of : 5
we : 5
can : 4
that : 4
you : 4
a : 3
are : 3
things : 3
programs : 2
is : 2
very : 2
many : 2
ranging : 2
from : 2
your : 2
know : 2
how : 2
what : 2
with : 2
We : 2
in : 2
on : 2
us : 2
would : 2
like : 2
could : 2
knew : 2
language : 2
computer : 2
kinds : 2
Writing : 1
(or : 1
programming) : 1
creative : 1
rewarding : 1
activity. : 1
You : 1
write : 1
for : 1
reasons : 1
making : 1
living : 1
solving : 1
difficult : 1
data : 1
analysis : 1
problem : 1
having : 1
fun : 1
helping : 1
someone : 1
else : 1
solve : 1
problem. : 1
This : 1
book : 1
assumes : 1
everyone : 1
needs : 1
program : 1
once : 1
program, : 1
will : 1
figure : 1
out : 1
want : 1
newfound : 1
skills. : 1
surrounded : 1
daily : 1
lives : 1
laptops : 1
cell : 1
phones. : 1
think : 1
these : 1
as : 1
“personal : 1
assistants” : 1
who : 1
take : 1
care : 1
behalf. : 1
The : 1
hardware : 1
current-day : 1
essentially : 1
built : 1
continuously : 1
ask : 1
question, : 1
“What : 1
me : 1
next?” : 1
Our : 1
fast : 1
have : 1
vast : 1
amounts : 1
memory : 1
be : 1
helpful : 1
if : 1
only : 1
speak : 1
explain : 1
it : 1
next. : 1
If : 1
this : 1
tell : 1
tasks : 1
behalf : 1
were : 1
repetitive. : 1
Interestingly, : 1
best : 1
often : 1
humans : 1
find : 1
boring : 1
mind-numbing. : 1
----------------------------------------------------------------------
to : 16
the : 6
and : 5
do : 5
our : 5
computers : 5
of : 5
we : 5
can : 4
that : 4
----------------------------------------------------------------------
to : 00016
the : 00006
we : 00005
our : 00005
of : 00005
do : 00005
computers : 00005
and : 00005
you : 00004
that : 00004
test.txt
-------------------------------
Writing programs (or programming) is a very creative and rewarding activity. You can write
programs for many reasons ranging from making your living to solving a difficult data analysis
problem to having fun to helping someone else solve a problem. This book assumes that
everyone needs to know how to program and that once you know how to program, you will figure
out what you want to do with your newfound skills.
We are surrounded in our daily lives with computers ranging from laptops to cell phones. We can
think of these computers as our “personal assistants” who can take care of many things on our
behalf. The hardware in our current-day computers is essentially built to continuously ask us the
question, “What would you like me to do next?”
Our computers are fast and have vast amounts of memory and could be very helpful to us if we
only knew the language to speak to explain to the computer what we would like it to do next. If
we knew this language we could tell the computer to do tasks on our behalf that were repetitive.
Interestingly, the kinds of things computers can do best are often the kinds of things that we
humans find boring and mind-numbing.
파일에서 가장 많이 사용하는 단어 찾기 및 정렬
|
2019.08.21 17:38:35
|
2019.08.22 20:13:31
|
342
|
Aiden
Total of Attached file
0.00 Bytes of 0 files
2019.08.29
2019.08.29
2019.08.28
2019.08.23
2019.08.23
2019.08.21
2019.08.21
2017.03.18
2017.03.01
2017.02.18
2017.02.18