网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
02月09日漏签0天
python吧 关注:481,306贴子:1,985,227
  • 看贴

  • 图片

  • 吧主推荐

  • 视频

  • 游戏

  • 0回复贴,共1页
<<返回python吧
>0< 加载中...

想问一个python里关于list的问题

  • 取消只看楼主
  • 收藏

  • 回复
  • Lemboorg
  • 白丁
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
最近在coursera上自学python,有一个作业不太懂,想问问大家,这是作业要求:
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. 即把一段文字拆分为单词,然后按照首字母排列,如果没有的单词再补上。
这是标准答案:
fname = input("Enter file name: ")
fh = open(fname)
lst = list()
words = list()
for line in fh:
words = line.split()
for word in words:
if lst.count(word) == 0:
lst.append(word)
lst.sort()
print(lst)
我的疑惑是:正确答案的code是怎么把重复的单词筛掉的呢?因为原文字中有重复的单词,但是出来的结果中没有。


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 0回复贴,共1页
<<返回python吧
分享到:
©2026 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示