I have a code as below
How can I find that abc is a list made up of lists?
Whats wrong with my map function?
I want my function to return count of each element in my input list divided by length of my list.
Something like
{'brown': 0.16666666666666666, 'lazy': 0.16666666666666666, 'jumps': 0.16666666666666666, 'fox': 0.16666666666666666, 'dog': 0.16666666666666666, 'quick': 0.16666666666666666}
My code:
quickbrownfox1=['quick', 'brown', 'fox', 'jumps', 'lazy', 'dog']
print quickbrownfox1
def tf(tokens):
abc=([[x,(tokens.count(x))] for x in set(tokens)])
print type(abc)#how to know that abc is made up of lists
print type(abc[1])
answer=abc.map(lambda input:(input(0)),input(1)/len(tokens)))
return answer
#return <FILL IN>
print tf((quickbrownfox1)) # Should give { 'quick': 0.1666 ... }
#print tf(tokenize(quickbrownfox)) # Should give { 'quick': 0.1666 ... }
_______________________________________
update 1
I updated my code as below. I get result [('brown', 0), ('lazy', 0), ('jumps', 0), ('fox', 0), ('dog', 0), ('quick', 0)] any idea why? If i do return return list(map(lambda input: (input[0], input[1]), abc)), it gives correct result - [('brown', 1), ('lazy', 1), ('jumps', 1), ('fox', 1), ('dog', 1), ('quick', 1)]
from __future__ import division
quickbrownfox1=['quick', 'brown', 'fox', 'jumps', 'lazy', 'dog']
def islistoflists(i):
if isinstance(i, list):
if len(i) > 0 and all(isinstance(t, list) for t in i):
return True
return False
def tf(tokens):
print(islistoflists(tokens))
abc = ([[x,tokens.count(x)] for x in set(tokens)])
return list(map(lambda input: (input[0], input[1] / len(tokens)), abc))
print tf(quickbrownfox1)
update 2
I am using pyspark/spark. Could that be a reason for issues that i am facing in update1?
is a list made up of lists? Make a for loop on abc then check every element withtype(). If all of them lists, then you got what you want.mapis a build-in function, not a method, soabc.mapit will not work, you have to use asmap(function, abc)