Python list count elements

advertisements

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?


The counter solution will definitely be better. Your use of tokens.count gives the code quadratic time complexity. Heres your code fixed up. You should note that map is a standalone function, not a member function of a list or any other type.

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)

To test if you have a list of lists, you can use isinstance to check the type of the parent object and if its a list and has at least one element in it, you can loop through them using isinstance to check if each child object is a list.

Note that I made your function return a list of tuples, implying that the items are read only, but you could make it return a list of lists by changing the line.

return list(map(lambda input: [input[0], input[1] / len(tokens)], abc))

If you look at it closely you'll see that a set of parenthesis have been substituted for square brackets, making each element a list.

If you have a older version of python 2 that does not support the from __future__ import division import you can uses the following workaround to force float division to occur.

return list(map(lambda input: (input[0], (input[1] * 1.0) / len(tokens)), abc))