Python basic experiment one

Python basic experiment one

Task 1:
Enter a set of data from the keyboard, write a program, and calculate the average, standard deviation, and median of the set of data. The output result retains
one decimal place.
Input sample 1
99 98 97 96 95
Output sample 1
avg: 97.0 variance: 1.6 median: 97.0
Input sample 2
99 98 97 96 95 94
output sample 2
avg: 96.5 variance: 1.9 median: 96.5

"""
# @Time    :  2020/4/9
# @Author  :  JMChen
"""
import numpy as np

list1 = []
list2 = []
list1 = str(input()).split(' ')
for i in list1:
    list2.append(int(i))
a = np.array(list2)
print(a)
# avg = sum(list2)/(len(list2))
print('avg:{0:.1f} variance:{1:.1f} median:{2:.1f}'.format(np.average(a), np.std(a), np.median(a)))

Task 2:
Programming requirements A
progress bar is an important means commonly used by computers to process tasks or execute software to enhance the user experience. It can display the
progress of tasks or software in real time .
At its simplest, you can use the print () function to implement a simple non-refreshing text progress bar. The basic idea is to
divide the entire task into 100 units according to the task execution percentage , and output a progress bar every time N% is executed. Each line of output contains a percentage of progress
, two characters








representing the completed part ( ) and uncompleted part (...), and a small arrow that follows the completion degree , the style is as follows: % 10 [****- > ...] in claim progress from the keyboard progress bar used for loop and print () body of the function configuration procedure, output one hundred points than the highest (100%) of 3-bit data, for output look neat, you may be used {: 3.0f} Format the percentage part. Input sample 1 10 Output sample 1 % 0 [->…] % 10 [ ->…]
% 20 [ ->…]
% 30 [
->…]
% 40 [
->…]
% 50 [ ->… ]
% 60 [
->…]
% 70 [
->…]
% 80 [ ->…]
% 90 [
->…]
%100[
************->]

The process has ended, exit code 0

"""
# @Time    :  2020/4/9
# @Author  :  JMChen
"""

# 非刷新的文本进度条
import time

x = eval(input())
for i in range(x + 1):
    a = "**" * i
    b = ".." * (x - i)
    c = i / x * 100
    print("%{:3.0f}[{}->{}]".format(c, a, b))

Task three:
programming requirements
statistical English word frequency, read an article from a file Text_word_frequency_statistics.txt, the output of the article is the most
commonly occurring words and appears 10 times, for the same number of times a word appears, then lexicographically keys For
sorting, you need to exclude the, and, of, a, i, in, you, my, he, and his. The output format is "{0: <10} {1:> 5}".
Tip: The
first step: decompose and extract words from English articles. The same word may have different forms of upper and lower case, but the count is not
case sensitive. All letters can be converted to lower case, to exclude the interference of the original text case on the word frequency statistics. English
words can be separated by spaces, punctuation marks, or special symbols. For a uniform separation method, you can replace various special characters
and punctuation marks with spaces, and then extract words.
Step 2: Count each word.
Part 3: Sort the word statistics from high to low. If the statistics are the same, sort them in ascending order by key value.

文件 Text_word_frequency_statistics.txt :

Salty Coffee
He met her at a party. She was outstanding; many guys were after her, but nobody paid any attention to him. After the party, he invited her for coffee. She was surprised. So as not to appear rude, she went along.
As they sat in a nice coffee shop, he was too nervous to say anything and she felt uncomfortable. Suddenly, he asked the waiter, “Could you please give me some salt? I’d like to put it in my coffee.”
They stared at him. He turned red, but when the salt came, he put it in his coffee and drank. Curious, she asked, “Why put salt in the coffee?” He explained, “When I was a little boy, I lived near the sea. I liked playing on the seaside … I could feel its taste salty, like salty coffee. Now every time I drink it, I think of my childhood and my hometown. I miss it and my parents, who are still there.”
She was deeply touched. A man who can admit that he’s homesick must love his home and care about his family. He must be responsible.
She talked too, about her faraway hometown, her childhood, her family. That was the start to their love story.
They continued to date. She found that he met all her requirements. He was tolerant, kind, warm and careful. And to think she would have missed the catch if not for the salty coffee!
So they married and lived happily together. And every time she made coffee for him, she put in some salt, the way he liked it.
After 40 years, he passed away and left her a letter which said:
My dearest, please forgive my life-long lie. Remember the first time we dated? I was so nervous I asked for salt instead of sugar.
It was hard for me to ask for a change, so I just went ahead. I never thought that we would hit it off. Many times, I tried to tell you the truth, but I was afraid that it would ruin everything.
Sweetheart, I don’t exactly like salty coffee. But as it mattered so much to you, I’ve learnt to enjoy it. Having you with me was my greatest happiness. If I could live a second time, I hope we can be together again, even if it means that I have to drink salty coffee for the rest of my life.

输出样例 1
to 12
coffee 11
it 11
she 11
was 11
her 8
for 7
salty 6
that 6
salt 5

"""
# @Time    :  2020/4/9
# @Author  :  JMChen
"""

with open('Text_word_frequency_statistics.txt', 'r')as f:
    f = f.read().lower()
    for ch in '!,".:?;':
        f = f.replace(ch, '')

l = f.split()
l.sort()
countDict = {}
for word in l:
    if word in ['the', 'and', 'of', 'a', 'i', 'in', 'you', 'my', 'he', 'his']:
        continue
    else:
        countDict[word] = countDict.get(word, 0) + 1
        
cList = list(countDict.items())
cList.sort(key=lambda x: x[1], reverse=True)

for i in range(10):
    w, c = cList[i]
    print('{0:<10}{1:>5}'.format(w, c))

Insert picture description here
Note: If the statistical values ​​are the same, they are arranged in ascending order according to the key value.

YES
Published 7 original articles · Like 8 · Visits 133

Guess you like

Origin blog.csdn.net/weixin_44983848/article/details/105421274