Assignment 8.4 Python Data Structures

Coursera:Python for Everybody Specialization

刚开始准备转码刷题,先入门一门语言python,跟的是Coursera上PYTHON FOR EVERYBODY 的专项课,目标是基本掌握语言可以简单抓包,在刷题的时候改Java格式完成刷题。

这个系列的课还是很容易上手的,for everybody嘛,一共有5门课程:

  • 1 Programming for Everybody (Getting Started with Python)

  • 2 Python Data Structure

  • 3 Using Python to Access Web Data

  • 4 Using Databases with Python(+SQL)

  • 5 Capstone: Retrieving, Processing, and Visualizing Data with Python

现在是第二门途中,遇到的第一个改了半个多点儿的程序,特此记录分享。

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

#fname = input("Enter file name: ")
##fh = open(fname)
##sum = list()
##for line in fh:
##    line=line.rstrip()
##    line=line.split()
##    sum=sum+line
##for i in range(len(sum)-1):
##    #print(i)
##    j=i+1
##    while j<len(sum)-1:
##        #print(j)
##        if sum[i]==sum[j]:
##            #print(sum[i])
##            
##            sum.remove(sum[i])
##            break
##        else:
##            j=j+1
##        
##sum.remove(sum[-6])
##sum.remove(sum[-7])
##sum.sort()
##print(sum)

一开始自己改来改去写了个很长的程序还总是错误,经过万能搜索发现了一个函数 set(),可以自动将程序中重复的元素删除,简简单单,是我愚昧,于是变成了这样。
(函数参考文章见https://blog.csdn.net/harry_128/article/details/80536305)

fname = input("Enter file name: ")
fh = open(fname)
sum = list()
lst = list()
for line in fh:
    line=line.rstrip()
    line=line.split()
    sum=sum+line
lst=list(set(sum))
lst.sort()
print(lst)

清清爽爽,第一次发博,再见~

哦对,sort()函数没有返回值,不能像lst=lst.sort()这样写,只能单独列一行,独行。

补10.2 assignment:
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.

name = input("Enter file:")
#if len(name) < 1 : name = "mbox-short.txt"
handle = open(name)
counts = dict()
for line in handle:
    if line.startswith('From:'):continue
    if line.startswith('From'):
        #print(line)
        line=line.rstrip()
        words=line.split()
        #print(words)
        time=words[5]
        hours=time.split(':')
        hours=hours[0]
        counts[hours] = counts.get(hours, 0) + 1
        #print(hours)
#print(counts)
lst = list()
for hour, count in counts.items():
    newtup = (hour, count)
    lst.append(newtup)
lst = sorted(lst)
for hour, count in lst[:]:
    print(hour, count)
        

. Which of the following methods work both in Python lists and Python tuples?

A reverse()

B pop()

C append()

D sort()

E index()

答案:E。元组没有ABCD,只有E,列表是全有。
reverse()(https://www.runoob.com/python/att-list-reverse.html)
pop()(https://www.runoob.com/python/python-att-dictionary-pop.html)
append()(https://www.runoob.com/python3/python3-att-list-append.html)
sort()(https://www.runoob.com/python/att-list-sort.html)index()(https://www.runoob.com/python/att-list-index.html)

转载上述QUIZ : https://blog.csdn.net/zjt1027/article/details/104076503

转载一位大哥的所有作业总结,常去看看!:https://blog.csdn.net/lulu_daisy/article/details/78637935

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值