对于我的编程类,我必须根据以下描述创建一个函数:The parameter is a tweet. This function should return a list containing all of the hashtags in the tweet, in the order they appear in the tweet. Each hashtag in the returned list should have the initial hash symbol removed, and hashtags should be unique. (If a tweet uses the same hashtag twice, it is included in the list only once. The order of the hashtags should match the order of the first occurrence of each tag in the tweet.)

我不确定如何使它在遇到标点时结束标签(参见第二个doctest示例)。我当前的代码没有输出任何内容:def extract(start, tweet):

""" (str, str) -> list of str

Return a list of strings containing all words that start with a specified character.

>>> extract('@', "Make America Great Again, vote @RealDonaldTrump")

['RealDonaldTrump']

>>> extract('#', "Vote Hillary! #ImWithHer #TrumpsNotMyPresident")

['ImWithHer', 'TrumpsNotMyPresident']

"""

words = tweet.split()

return [word[1:] for word in words if word[0] == start]

def strip_punctuation(s):

""" (str) -> str

Return a string, stripped of its punctuation.

>>> strip_punctuation("Trump's in the lead... damn!")

'Trumps in the lead damn'

"""

return ''.join(c for c in s if c not in '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~')

def extract_hashtags(tweet):

""" (str) -> list of str

Return a list of strings containing all unique hashtags in a tweet.

Outputted in order of appearance.

>>> extract_hashtags("I stand with Trump! #MakeAmericaGreatAgain #MAGA #TrumpTrain")

['MakeAmericaGreatAgain', 'MAGA', 'TrumpTrain']

>>> extract_hashtags('NEVER TRUMP. I'm with HER. Does #this! work?')

['this']

"""

hashtags = extract('#', tweet)

no_duplicates = []

for item in hashtags:

if item not in no_duplicates and item.isalnum():

no_duplicates.append(item)

result = []

for hash in no_duplicates:

for char in hash:

if char.isalnum() == False and char != '#':

hash == hash[:char.index()]

result.append()

return result

在这一点上我很迷茫,任何帮助都将不胜感激。提前谢谢你。在

注意:我们不允许使用正则表达式或导入任何模块。在

python中的结束用语,python语句结束符号_Python从文本中提取hashtags;以标点符号结尾...相关推荐

  1. python语句结束符号_Python从文本中提取hashtags;以标点符号结尾

    对于我的编程类,我必须根据以下描述创建一个函数:The parameter is a tweet. This function should return a list containing all ...

  2. python强制用什么作为语句缩进符号_python从入门到放弃 第二天 谈谈python代码中的冒号和缩进...

    如同格子衬衫一样规整的源代码书写层次,是一个优秀的码农必备的编程习惯.python的作者无意就是这样一个人. python是用冒号(:)和强制缩进作为代码层次来划分代码组的.这是一个很小的知识点,但是 ...

  3. python输出文本和值_python读取文本中数据并转化为DataFrame的实例

    在技术问答中看到一个这样的问题,感觉相对比较常见,就单开一篇文章写下来. 从纯文本格式文件 "file_in"中读取数据,格式如下: 需要输出成"file_out&quo ...

  4. python单词个数统计_Python 统计文本中单词的个数

    1.读文件,通过正则匹配 def statisticWord(): line_number = 0 words_dict = {} with open (r'D:\test\test.txt',enc ...

  5. python统计文件中每个单词出现的次数_python统计文本中每个单词出现的次数

    .python统计文本中每个单词出现的次数: #coding=utf-8 __author__ = 'zcg' import collections import os with open('abc. ...

  6. 【vim】系统剪切板、vim寄存器之间的复制粘贴操作命令?系统剪切板中的内容复制粘贴到命令行?vim文本中复制粘贴到命令行

    一.系统剪切板和文本内容的复制粘贴 1.1 从系统剪切板复制粘贴到文本中 需要操作3次: 分别是英文双引号.一个加号或梅花号,最后是一个p 也即"+p 或者直接使用组合键[Shift + i ...

  7. python统计字符出现的次数最多的字符_Python统计字符串中出现次数最多的人名

    人名最多数统计 题目摘自https://python123.io 描述 编程模板中给出了一个字符串,其中包含了含有重复的人名,请直接输出出现最多的人名.‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬ ...

  8. python读取txt中的一列称为_Python读取文本中的数字

    作者:金良(golden1314521@gmail.com) csdn博客:http://blog.csdn.net/u012176591 1.例程1 先看文本内容形式: 1404796773468 ...

  9. python显示当前中文日期_python自动提取文本中的时间(包含中文日期)

    有时在处理不规则数据时需要提取文本包含的时间日期. dateutil.parser模块可以统一日期字符串格式. datefinder模块可以在字符串中提取日期. datefinder模块实现也是用正则 ...

最新文章

  1. Kraken2 物种序列比对 注释
  2. ASP的Server.UrlEncode和Asp.Net的Server.UrlEncode的返回结果不同
  3. python-之基本语法
  4. 什么是Servlet?
  5. 没有RunInstallerAttribute.Yes的公共安装程序。
  6. bst latex 最大作者数_latex 参考文献作者是三个以上时如何处理?
  7. [JavaWeb-HTML]HTML标签_表格标签
  8. vim 高亮显示php代码
  9. 25k英里高速建48个充电走廊,美国电动汽车产业迎来春天
  10. HDU1875 畅通工程再续【Kruskal算法+并查集】
  11. 内容提供者和内容观察者
  12. 前端性能优化(十一)
  13. Java获取打印机打印图片
  14. android平板安装win10,如何给平板电脑装win10系统?
  15. ML笔记 - 自然语言处理常用技术
  16. 初始JavaScript
  17. 小心啦!水上有“天眼”,查你没商量!
  18. 简单的文本挖掘-用于QQ聊天记录(R)
  19. (16)调度代码周期性运行
  20. AI的不封闭图形填色

热门文章

  1. Python技巧——解析式
  2. WebRTC音视频采集和播放示例及MediaStream媒体流解析
  3. linux下开机自动启动,定时运行shell脚本
  4. Android Gradle Composing builds 管理三方依赖
  5. Redis 集群搭建及集群管理工具
  6. 概率论:乘法定理、全概率公式以及贝叶斯定理
  7. STC8H开发(十五): GPIO驱动Ci24R1无线模块
  8. U盘制作-BGA焊接练习
  9. 读书笔记:Spring in action 第2章
  10. 数据结构之顺序表的删除、查找、遍历