对于我的编程类,我必须根据以下描述创建一个函数: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从文本中提取hashtags;以标点符号结尾相关推荐

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

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

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

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

  3. python批量读取文件名_python - 从路径中提取文件名,无论os / path形式如何

    python - 从路径中提取文件名,无论os / path形式如何 无论操作系统或路径格式是什么,我可以使用哪个Python库从路径中提取文件名? 例如,我想要所有这些路径返回c: a/b/c/ a ...

  4. python计算一个多项式_Python 从一元多项式中提取系数和次数,并进行简单的运算...

    import re #正则 from collections import defaultdict #defaultdict #提取一元多项式(type: str)中的次数和系数并转化为字典 -> ...

  5. python 多项式求系数_Python 从一元多项式中提取系数和次数,并进行简单的运算...

    import re #正则 from collections import defaultdict #defaultdict #提取一元多项式(type: str)中的次数和系数并转化为字典 -> ...

  6. python中 for i in range(10)_在Python语句: for i in range(10) : ...... 中,循环终值是( )。...

    在Python语句: for i in range(10) : ...... 中,循环终值是( ). 更多相关问题 是导致成本结构发生变化的重要因素.A.竞争优势B.生产规模C.成本动因D.经营过程的 ...

  7. python --cpca(从文本中提取省市区)

    安装 pip install cpcawindows报错的话 c++构建工具补全下动态链接库 或 pip install cpca-fix(替代) github https://github.com/ ...

  8. python:实现从img及其坐标中提取文本(附完整源码)

    python:实现从img及其坐标中提取文本 import cv2 import pytesseract img = cv2.imread("img.png") # We need ...

  9. python 办公常用一:从文本文件中提取手机号码

    https://blog.csdn.net/A757291228/article/details/117464313 python 办公常用 一.从文本文件中提取手机号码 给定一个文本文件从中提取所有 ...

最新文章

  1. (转)创业需要知道的13句话
  2. 把Sublime Text 2打造成一个轻量级Python的IDE
  3. 前端学习(3267):js中this在类中的表现
  4. visual assist安装方法
  5. mysql连接参数配置
  6. JAVA netty 定时任务_Netty HashedWheelTimer 定时任务调用
  7. autobench 快速入门
  8. 阿里云centos7监控postgres9.6.6
  9. 人脸对齐(十八)--Joint Face Alignment and 3D Face Reconstruction
  10. 春节7天新增病毒54万 钓鱼欺诈成最大威胁
  11. 两个简洁的页面:404和Loading
  12. 捷联惯导姿态解算基础
  13. 扩展欧几里得算法 思想及模板代码
  14. c语言编程一个登陆界面设计,怎么用C语言编写个登陆界面?
  15. 程序人生-hit计统大作业
  16. 支付宝扫码支付-PC版(沙箱环境)
  17. 基于TransferNet和nlpcc2018知识图谱搭建问答服务
  18. 三个路由器三个pc实现全网可达
  19. stm32f103c8t6调试-while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET );卡死
  20. 常用工具网址(去重,排序,转换)

热门文章

  1. 海思平台的拍照和拍照后期处理算法的介绍
  2. 实际项目中的消息中心
  3. 计算机视觉博士去向,为什么现在不看好 CV 方向了呢?
  4. 全新的3D采样捕捉工具:Substance 3D Sampler(Sa) mac中文版
  5. 魔法少女小圆计算机音乐,魔法少女小圆 无损音乐合集
  6. html爆炸效果,HTML5 SVG炫酷文字爆炸特效
  7. 公有iot私有iot架构_玩洋葱Omega IoT设备在OLED屏幕上显示实时血糖
  8. win10系统下jdk环境变量配置
  9. JQuery开发手册
  10. hey-cli初使用