python - 如何使用 MapReduce 方法提取 MrJob 中的行索引?
问题描述
您如何提取 MrJob 中任何给定行的行索引?
index_words = ["before", "remove"]
class MRWordInvertedIndex(MRJob):
# how to make the key(index) the line index of the corresponding value(line) in the input text file?
def mapper(self, index, line):
words = WORD_RE.findall(line.lower())
for word in words:
# obtain the line index where 'word' occurs
if word in index_words:
yield word.lower(), index # where index is the line number
是否可以使index
映射器的 key(aka ) 参数成为相应输入文本文件中行的实际行索引或以其他方式获取行索引?
例如,假设输入文本文件是:
# copyright laws for your country before downloading or redistributing
# this or any other Project Gutenberg eBook. BLANK LINE BELOW.
# This header should be the first thing seen when viewing this Project
# Gutenberg file. Please do not remove it.
然后索引词的行索引before
和remove
将是:
"before": 1
"remove": 4
由于before
发生在第 1 行并remove
发生在第 4 行。
解决方案
推荐阅读
- kotlin - libgdx holo-skin: no No SelectBoxStyle 注册名称:默认
- javascript - 如何在文档头中一个接一个地附加一个元素?
- java - session.merge() 在休眠中没有按预期工作
- browser - 浏览器(例如通过 XMLHttpRequest API)作为“代理”转发 HTTPS
- python - 如何通过在 discord.py 中使用另一种方法来更改方法的参数值
- azure - Azure:Python SDK 和 KeyVault——服务主体
- proxy - Traefik 重写位置响应头
- xml - 如何在电源自动化桌面中获取多个 xml 元素属性?
- jboss - 使用 JBoss 7.3 部署 GeoServer 2.17.2 时出错
- reactjs - 在进行 Jest 测试时处理内部 Promise