Python Data Structures Assignment答案

This course will introduce the core data structures of the Python programming language. We will move past the basics of procedural programming and explore how we can use the Python built-in data structures such as lists, dictionaries, and tuples to perform

6.5

Write code using find() and string slicing (see section 6.10) to extract the number at the end of the line below. Convert the extracted value to a floating point number and print it out.

text = "X-DSPAM-Confidence:    0.8475"
p1 = text.find('0')
p2 = text.find('5')
num = text[p1: p2+1]
n = float(num)
print(n)

Desired Output

0.8475

7.1

Write a program that prompts for a file name, then opens that file and reads through the file, and print the contents of the file in upper case. Use the file words.txt to produce the output below.

You can download the sample data at http://www.py4e.com/code3/words.txthttps://www.py4e.com/code3/words.txt

# Use words.txt as the file name
fname = input('Enter the name:')
fh = open(fname)
iny = fh.read()
iny= iny.rstrip()
print(iny.upper())

Desired Output

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
{\EM 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 AS US THE QUESTION
WHAT WOULD YOU LIKE ME TO DO NEXT

OUR COMPUTERS ARE FAST AND HAVE VASTS 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 REPTITIVE
INTERESTINGLY, THE KINDS OF THINGS COMPUTERS CAN DO BEST
ARE OFTEN THE KINDS OF THINGS THAT WE HUMANS FIND BORING
AND MIND-NUMBING


7.2

Write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form:

X-DSPAM-Confidence:    0.8475

Count these lines and extract the floating point values from each of the lines and compute the average of those values and produce an output as shown below. Do not use the sum() function or a variable named sum in your solution.

You can download the sample data at http://www.py4e.com/code3/mbox-short.txthttps://www.py4e.com/code3/mbox-short.txt?PHPSESSID=01888bb1a18690edaeded43edd9110ad when you are testing below enter mbox-short.txt as the file name. 

# Use the file name mbox-short.txt as the file name
fname = input('Enter file name:')
fh = open(fname)
s = 0
n = 0
for line in fh :
    if not line.startswith('X-DSPAM-Confidence:'):
        continue
    p = line.find('0')
    str1 = line[p:]
    num = float(str1)
    s += num
    n += 1
aver = s / n
print("Average spam confidence:", aver)

Desired Output

Average spam confidence: 0.7507185185185187

8.4

Open the file romeo.txt and read it line by line. For each line, split the line into a list of words using the split() method. The program should build a list of words. For each word on each line check to see if the word is already in the list and if not append it to the list. When the program completes, sort and print the resulting words in python sort() order as shown in the desired output.

You can download the sample data at http://www.py4e.com/code3/romeo.txthttps://www.py4e.com/code3/romeo.txt?PHPSESSID=4f9bfb0979a24fb3ccdb7fa64419d841

fname = input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh :
	listofword = line.split()
	for word in listofword :
		if word not in lst :
			lst.append(word)
lst.sort()
print(lst)

Desired Output

['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']

8.5 

Open the file mbox-short.txt and read it line by line. When you find a line that starts with 'From ' like the following line:

From stephen.marquard@uct.ac.za Sat Jan  5 09:14:16 2008

You will parse the From line using split() and print out the second word in the line (i.e. the entire address of the person who sent the message). Then print out a count at the end.

 Hint: make sure not to include the lines that start with 'From:'. Also look at the last line of the sample output to see how to print the count.

You can download the sample data at http://www.py4e.com/code3/mbox-short.txthttps://www.py4e.com/code3/mbox-short.txt?PHPSESSID=b4616915ad2cb387edf54c15ddfcf06c

fname = input('Enter file name : ')
if len(fname) < 1 :
    fname = "mbox-short.txt"
fh = open(fname)
count = 0
for line in fh :
    if line.startswith('From ') :
        piece = line.split()
        email = piece[1]
        print(email)
        count += 1
    else :
        pass
print("There were", count, "lines in the file with From as the first word")

Desired Output

stephen.marquard@uct.ac.za
louis@media.berkeley.edu
zqian@umich.edu
rjlowe@iupui.edu
zqian@umich.edu
rjlowe@iupui.edu
cwen@iupui.edu
cwen@iupui.edu
gsilver@umich.edu
gsilver@umich.edu
zqian@umich.edu
gsilver@umich.edu
wagnermr@iupui.edu
zqian@umich.edu
antranig@caret.cam.ac.uk
gopal.ramasammycook@gmail.com
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
stephen.marquard@uct.ac.za
louis@media.berkeley.edu
louis@media.berkeley.edu
ray@media.berkeley.edu
cwen@iupui.edu
cwen@iupui.edu
cwen@iupui.edu
There were 27 lines in the file with From as the first word


9.4 

Write a program to read through the mbox-short.txt and figure out who has sent the greatest number of mail messages. The program looks for 'From ' lines and takes the second word of those lines as the person who sent the mail. The program creates a Python dictionary that maps the sender's mail address to a count of the number of times they appear in the file. After the dictionary is produced, the program reads through the dictionary using a maximum loop to find the most prolific committer. 

 Hint: make sure not to include the lines that start with 'From:'. Also look at the last line of the sample output to see how to print the count.

You can download the sample data at http://www.py4e.com/code3/mbox-short.txthttps://www.py4e.com/code3/mbox-short.txt?PHPSESSID=b4616915ad2cb387edf54c15ddfcf06c

fname = input('Enter file name : ')
if len(fname) < 1 :
    fname = "mbox-short.txt"
fh = open(fname)

count = dict()
for line in fh :
    if line.startswith('From ') :
        words = line.split()
        email = words[1]
        count[email] = count.get(email, 0) + 1

maxcount = None
maxname = None
for k, v in count.items() :
    if maxcount == None or v > maxcount :
        maxcount = v
        maxname = k
print(maxname, maxcount)

 Desired Output

cwen@iupui.edu 5

10.2 

Write a program to read through the mbox-short.txt and figure out the distribution by hour of the day for each of the messages. You can pull the hour out from the 'From ' line by finding the time and then splitting the string a second time using a colon.

From stephen.marquard@uct.ac.za Sat Jan  5 09:14:16 2008

Once you have accumulated the counts for each hour, print out the counts, sorted by hour as shown below. 

 Hint: make sure not to include the lines that start with 'From:'. Also look at the last line of the sample output to see how to print the count.

You can download the sample data at http://www.py4e.com/code3/mbox-short.txthttps://www.py4e.com/code3/mbox-short.txt?PHPSESSID=b4616915ad2cb387edf54c15ddfcf06c

name = input('Enter file: ')
if len(name) < 1 :
    name = 'mbox-short.txt'
hand = open(name)

dic = {}
for line1 in hand :
    if line1.startswith('From ') :
        words1 = line1.split()
        time = words1[5]
        words2 = time.split(':')
        hour = words2[0]
        dic[hour] = dic.get(hour, 0) + 1

for k, v in sorted(dic.items(), reverse=False) :
    print(k, v)

Desired Output

04 3
06 1
07 1
09 2
10 3
11 6
14 1
15 2
16 4
17 2
18 1
19 1

你可能感兴趣的:(Python,for,Everybody,python,开发语言)