【Python for Everybody(Python Data Structures)】Week 4 | Chapter 8 题目汇总

PY4E课程官网:https://www.py4e.com/

参考文章(Github):

  1. ed-lau/python-for-everybody
  2. kalpesh14m/Python-For-Everybody-Answers











Assignment 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 alphabetical order.
You can download the sample data at http://www.py4e.com/code3/romeo.txt    

打开文件romeo.txt,逐行阅读。对于每一行,使用split()方法将其分割成一个单词列表。程序应该建立一个单词列表。对于每行中的每个单词,检查该单词是否已经在列表中,如果没有,则将其追加到列表中。当程序完成后,按字母顺序对产生的单词进行排序并打印。你可以在 http://www.py4e.com/code3/romeo.txt 下载样本数据。

import sys
fname = input('Enter the file name:  ')
try:
    fhand = open(fname)
except:
    print(fname, 'File does not exist ')
    sys.exit()

words_list = list()

for line in fhand:
    line_spilt_list = line.split()
    for word in line_spilt_list:
        if not word in words_list:
            words_list.append(word)
            
words_list.sort()
print(words_list)

>>>Enter the file name:  romeo.txt
['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']

Assignment 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:'.  
You can download the sample data at http://www.py4e.com/code3/mbox-short.txt  

打开文件mbox-short.txt,逐行阅读。当你发现一行以'From'开头的行,比如下面这行。  
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008.
你将使用split()解析From行,并打印出行中的第二个字(即发送消息的人的全部地址)。然后在最后打印出一个计数。  
提示:确保不包括以'From:'开头的行。  
你可以在 http://www.py4e.com/code3/mbox-short.txt 下载样本数据。

import sys
fname = input('Enter the file name:  ')
try:
    fhand = open(fname)
except:
    print(fname, 'File does not exist ')
    sys.exit()

sender_list = list()

for line in fhand:
    line_spilt_list = line.strip().split()
    # line startswith 'From'  and not include 'From:'a
    if line_spilt_list:
        if line_spilt_list[0] == 'From':
            sender_list .append(line_spilt_list[1])

print(sender_list)
print("The length of the sender list is :", len(sender_list))

>>>Enter the file name:  mbox-short.txt
['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']
The length of the sender list is : 27

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值