当前位置: 首页 > news >正文

上海网站建设公司哪家好百度应用市场官网

上海网站建设公司哪家好,百度应用市场官网,如何在网上做网站推广,易读网站建设文章目录 问题:搜索特定单词并定位思路代码实现官方代码代码解析 更进一步 问题:搜索特定单词并定位 一位研究人员收集了数千篇新闻文章。但她想将注意力集中在包含特定单词的文章上。完成以下功能以帮助她过滤文章列表。 您的函数应满足以下条件&…

文章目录

    • 问题:搜索特定单词并定位
    • 思路
      • 代码实现
      • 官方代码
      • 代码解析
    • 更进一步

问题:搜索特定单词并定位

一位研究人员收集了数千篇新闻文章。但她想将注意力集中在包含特定单词的文章上。完成以下功能以帮助她过滤文章列表。

您的函数应满足以下条件:

不要包含关键字字符串仅作为较大单词的一部分出现的文档。例如,如果她正在查找关键字“close”,则您不会包含字符串“enlined”。
她不希望你区分大小写字母。所以这句话“结案了”。当关键字“关闭”时将被包含
不要让句号或逗号影响匹配的内容。 “已经关门了。”当关键字为“close”时将被包含。但您可以假设没有其他类型的标点符号

思路

  1. 读取列表中的字符串并转为小写
  2. 去除两边的干扰符号",.?",使用strip()函数
  3. 将中间的逗号替换为空格使用split()函数划分为单词
  4. 然后将划分出的单词与keyword进行比对,如果在则在空列表中保存索引
  5. 返回结果列表
# doc_list = ["The Learn Python Challenge Casino.", "They bought a car", "Casinoville"]
doc_list=['The Learn Python Challenge Casino', 'They bought a car, and a horse', 'Casinoville?']
keyword = 'Casino'
list = []
l = len(doc_list)
for i in range(l):words = doc_list[i].lower()print(words)words = words.strip('.,?')print(words)wordlist = words.replace(",","").split()print(wordlist)for word in wordlist:if word == keyword.lower():list.append(i)print(i)
#         if keyword in wordlist:
#             print(i)
print(list)

在这里插入图片描述

代码实现

def word_search(doc_list, keyword):"""Takes a list of documents (each document is a string) and a keyword. Returns list of the index values into the original list for all documents containing the keyword.Example:doc_list = ["The Learn Python Challenge Casino.", "They bought a car", "Casinoville"]>>> word_search(doc_list, 'casino')>>> [0]"""list = []l = len(doc_list)for i in range(l):words = doc_list[i].lower()words = words.strip(',.?')wordlist = words.replace(",","").split()for word in wordlist:if word == keyword:list.append(i)breakreturn list

官方代码

def word_search(doc_list, keyword):# list to hold the indices of matching documentsindices = [] # Iterate through the indices (i) and elements (doc) of documentsfor i, doc in enumerate(doc_list):# Split the string doc into a list of words (according to whitespace)tokens = doc.split()# Make a transformed list where we 'normalize' each word to facilitate matching.# Periods and commas are removed from the end of each word, and it's set to all lowercase.normalized = [token.rstrip('.,').lower() for token in tokens]# Is there a match? If so, update the list of matching indices.if keyword.lower() in normalized:indices.append(i)return indices

代码解析

enumerate() 是 Python 的一个内置函数,用于为可迭代对象(如列表、元组或字符串)提供一个自动计数器,同时遍历该对象。它返回一个包含索引和值的迭代器,常用于 for 循环中。
enumerate(iterable, start=0)

  • iterable: 任何可以遍历的对象,如列表、字符串等。
  • start(可选): 计数的起始值,默认为 0,也可以指定其他起始值。
  • enumerate() 返回一个迭代器对象,每次迭代返回一个元组,包含当前元素的索引和元素值。
  • 向字典中添加键值对(元素对)
    dictionary[key] = value
    • key:表示字典的键。
    • value:表示该键对应的值。
      在这里插入图片描述
  • str.split() 方法用于根据指定的分隔符将字符串拆分为子字符串列表。默认情况下,分隔符是任意的空白字符(空格、制表符或换行符)
    string.split(separator, maxsplit)
    • separator(可选): 指定的分隔符字符串。如果没有提供,字符串会按空白字符进行拆分。
    • maxsplit(可选): 指定最大拆分次数。默认值是 -1,表示不限制拆分次数。
  • str.rstrip() 是 Python 中的一个字符串方法,用于删除字符串末尾的指定字符(默认为空白字符)。
    string.rstrip([chars])
    • chars(可选): 指定要移除的字符序列。如果没有提供,默认会移除末尾的所有空白字符(包括空格、换行符、制表符等)。
  • str.strip() 是 Python 中用于删除字符串两端(开头和结尾)指定字符(默认为空白字符)的一个方法。它可以同时移除字符串开头和末尾的字符。
    string.strip([chars])
    • chars(可选): 指定要移除的字符序列。如果没有提供,默认会移除两端的所有空白字符(如空格、换行符、制表符等)。
    • result = text.strip(“,。?”) # 删除两端的 ‘,’、‘。’、‘?’

更进一步

现在研究人员想要提供多个关键字进行搜索。完成下面的函数来帮助她。

(我们鼓励您在实现此函数时使用刚刚编写的word_search函数。以这种方式重用代码可以使您的程序更加健壮和可读 - 并且可以节省打字!)
1、在里面改写函数,使用循环对多个keywords进行判断

def multi_word_search(doc_list, keywords):"""Takes list of documents (each document is a string) and a list of keywords.  Returns a dictionary where each key is a keyword, and the value is a list of indices(from doc_list) of the documents containing that keyword>>> doc_list = ["The Learn Python Challenge Casino.", "They bought a car and a casino", "Casinoville"]>>> keywords = ['casino', 'they']>>> multi_word_search(doc_list, keywords){'casino': [0, 1], 'they': [1]}"""# list to hold the indices of matching documents
#     indices = []dictionary = {}for keyword in keywords:indices = []# Iterate through the indices (i) and elements (doc) of documentsfor i, doc in enumerate(doc_list):# Split the string doc into a list of words (according to whitespace)tokens = doc.split()# Make a transformed list where we 'normalize' each word to facilitate matching.# Periods and commas are removed from the end of each word, and it's set to all lowercase.normalized = [token.rstrip('.,').lower() for token in tokens]# Is there a match? If so, update the list of matching indices.if keyword.lower() in normalized:indices.append(i)dictionary[keyword] = indicesreturn dictionary# Check your answer
q3.check()

2、直接调用前面已经实现的函数word_search(doc_list, keyword)

def multi_word_search(doc_list, keywords):"""Takes list of documents (each document is a string) and a list of keywords.  Returns a dictionary where each key is a keyword, and the value is a list of indices(from doc_list) of the documents containing that keyword>>> doc_list = ["The Learn Python Challenge Casino.", "They bought a car and a casino", "Casinoville"]>>> keywords = ['casino', 'they']>>> multi_word_search(doc_list, keywords){'casino': [0, 1], 'they': [1]}"""keyword_to_indices = {}for keyword in keywords:keyword_to_indices[keyword] = word_search(doc_list, keyword)return keyword_to_indices
http://www.ds6.com.cn/news/98225.html

相关文章:

  • 郑州微信网站建设营销软文范例大全300
  • 睢宁网站建设中国十大搜索引擎排名
  • 广西南宁做网站南京网络推广平台
  • 上饶市建设局官方网站seo网络推广经理
  • 制作小企业网站太仓网站制作
  • 做网站龙岗电脑系统优化软件排行榜
  • 好的建网站的书籍谷歌手机版浏览器官网
  • 来年做啥网站致富百度百家号怎么赚钱
  • 广西网站制作手机自己怎么建电影网站
  • 做物流的网站都有什么风险获客软件
  • 美国做刀剑的网站广州今日头条新闻
  • 绍兴网络科技有限公司东莞seo整站优化
  • 带屏蔽的网站做水晶头seo关键词优化软件怎么样
  • 微商网站如何做推广帮忙推广的平台
  • 有什么值得做的网站建设网站制作公司
  • 公司独立网站平台建设sem是什么意思职业
  • 门户网站系统开发广州信息流推广公司
  • 手机怎么做淘客网站网站建设与维护
  • 成都学生网站制作网络做推广广告公司
  • 免费 free 服务器 终身 永久东莞百度seo关键词优化
  • 成功的网站建设商丘seo教程
  • 营销型网站策划 建设的考试题seo排名赚挂机赚钱软件下载
  • 南京维露斯网站建设软文推广代理平台
  • 高德地图不显示菲律宾苏州seo服务热线
  • ruby网站开发工程师b站官方推广
  • 开发制作小程序公司手机百度关键词优化
  • 直销网站建设网站长沙百度网站排名优化
  • 江苏网站建设多少钱推广小程序
  • 云南网站建设天软科技有道搜索
  • 新闻门户网站什么意思百度知道怎么赚钱