使用MapReducer MRJob和我的mapper函数导致索引错误:列表索引超出范围。

2024-10-01 22:41:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我不熟悉MapReduce MRJob(老实说,我也不熟悉Python)。我试图使用MRJob来计算文本文件中不同列中从“A”到“E”的成对字母组合的数量,即“A”,“A”=10次出现,“A”,“B”=13次出现,“C”,“E”=6次出现,等等。我运行它时得到的错误是“列表索引超出范围”,我一直都不明白为什么

下面是一个文本文件示例,该文本文件与python mapreduce文件一起使用,带有mapper和reducer函数(顺便说一句,字符串包含日期、时间、电话持续时间、打电话的人的客户ID,以字母“a”到“E”开头,字母表示国家,接听电话的人的另一个客户ID和对话中的关键词)。我将字符串分解为一个列表,并在映射器中指出我感兴趣的索引,但我不确定这种方法是否正确:

Details
2020-03-05 # 19:28 # 5:10 # A-466 # C-563 # tendremos lindo ahi fuimos derecho carajo junto acabar
2020-03-10 # 05:08 # 5:14 # C-954 # D-353 # carajo calle película acaso voz creía irá san montón ambos hablas empieza estaremos parecía mitad estén vuelto música anoche tendremos tenían dormir habitación encuentra ésa
2020-01-15 # 09:47 # 4:46 # C-413 # B-881 # pudiera dejes querido maestro hacerle llamada paz estados estuviera hablo decirle bonito linda blanco negro querida hacerte dormir empieza mayoría
2020-01-10 # 20:54 # 4:58 # E-027 # A-549 # estuviera tuviste vieja volvió solía alrededor decía maestro estaremos línea sigues
2020-03-17 # 21:38 # 5:21 # C-917 # D-138 # encima música barco tuvimos dejes damas boca 

以下是python文件的完整代码:

from mrjob.job import MRJob

class MRduracion_llamadas(MRJob):
    def mapper(self, _, line):

        """
         First we need to convert the string from the text file into a list and eliminate the 
         unnecessary characters, such as "#", "-", ":", which I have substituted with a ";" to 
         facilitate the "split"part of this process. 
        """
        table = {35 : 59, 45 : 59, 58 : 59}
        llamadas2020_text_line = [column.strip() for column in \
                                 (line.translate(table)).split(";")]
        #Now we can assign values to "Key" and "Values"
        print(line)
        pais_emisor = llamadas2020_text_line[7]
        pais_receptor = llamadas2020_text_line[9]
        minutos = ""

        #If a call is "x" minutes and "y" secs long, where y > 0, then we can round up 
        #the minutes by 1 minute.

        if int(llamadas2020_text_line[6]) > 0:
            minutos = int(llamadas2020_text_line[5]) + 1
        else:
            minutos = int(llamadas2020_text_line[5])

        yield (pais_emisor, pais_receptor), minutos

    def reducer(self, key, values):
        yield print(key, sum(values))


if __name__ == "__main__":
        MRduracion_llamadas.run()

Tags: and文件thetotext列表lineint

热门问题