SoFunction
Updated on 2024-12-10

Python to determine the number of times a message is repeated in text

This article example describes the method of Python to determine the number of times a message is repeated in the text. Shared for your reference, as follows:

#coding:gbk
'''
Created on 2012-2-3
Read text from a file and determine how many messages such as "message0" and "message123" are duplicates in the text.
@author: Administrator
'''
import re
if __name__ == '__main__':
  pattern = u"(message((\d)+))"
  prog = (pattern)
  # read text from file
  f = open("","r")
  text = unicode(())
  ()
  result = (text)
  message_map = dict()
  redupicate_count = 0
  for message in result:
    if message_map.has_key(message[0]) == True:
      print message[0], "is reduplicate"
      redupicate_count += 1
    else :
      message_map[message[0]] = 1;
  print "total reduplicate message is ", redupicate_count

Readers interested in more Python related content can check out this site's topic: thePython Data Structures and Algorithms Tutorial》、《Python Socket Programming Tips Summary》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Python introductory and advanced classic tutorialsand theSummary of Python file and directory manipulation techniques

I hope that the description of this article will be helpful for you Python Programming.