[Tutor] Fw: Issues With Map Reduce

StevenJ Hennessy stevenhennessy at sbcglobal.net
Mon Sep 26 19:00:12 CEST 2011


Hello,


    I am currently struggling with a homework assignment. I need to use Map 
reduce to create a dictionary of palendromes -> number of palendrome 


for example: the string "bab bab bab cab cac dad" would output:

bab 3
cab 1
dad 1

here is what I have so far
def palendrome(string):
   palendromes = []
   for word in string.split(" "):
      if (word == word[::-1]):
              palendromes.append(word)
   return palendromes 

string = "abc abd bab tab cab tat yay uaefdfdu"
print map(lambda x: palendrome(x), ["bab abc dab bab bab dad crap pap pap "])

#returns a list of lists [['bab', 'bab', 'bab', 'dad', 'pap', 'pap', '']]

#Here is my attempt so far at the reduce section

def p(lists):
   for list in lists:
    
     set_h = set(list) 
  
   return set_h

#with the p function I want to create a set of all palendromes found. Then run a 
count of the palendroms on the list and make a dict #out of this
  
print reduce(p, [['bab', 'bab', 'bab', 'dad', 'pap', 'pap', '']])

 
----------------------
 
Am I on the right track?
 
Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110926/97b98b4b/attachment.html>


More information about the Tutor mailing list