Python MapReduce help

Joined
Sep 26, 2011
Messages
1
Reaction score
0
Hello,

I am currently struggling with an 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 "])

# current prints [['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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top