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

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top