0

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?

2
  • is a list made up of lists ? Make a for loop on abc then check every element with type(). If all of them lists, then you got what you want. Commented Feb 26, 2016 at 3:17
  • 1
    map is a build-in function, not a method, so abc.map it will not work, you have to use as map(function, abc) Commented Feb 26, 2016 at 3:44

3 Answers 3

1

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))
Sign up to request clarification or add additional context in comments.

3 Comments

I tried your approach. I get an answer [('brown', 0), ('lazy', 0), ('jumps', 0), ('fox', 0), ('dog', 0), ('quick', 0)] when i use list(map(lambda input: (input[0], int(input[1])/len(tokens) ), abc)). My abc is [['brown', 1], ['lazy', 1], ['jumps', 1], ['fox', a], ['dog', 1], ['quick', 1]]
i am follwoing a MOOC and pyspark. Not sure about the python version!
Use float(input[1]) to force float division.
0

Based on what I think you're asking you could do something like

token_size = len(tokens)
word_counter_list = {}
for word in tokens:
    if word in word_counter_list:
        word_counter_list[word] += 1
    else:
        word_counter_list[word] = 1

for word, amount in word_counter_list:
    print("The word " + word + " was used " + str(amount/token_size)

That being said the question isn't very clear since you're mentioning list type() but showing percentage of word frequency in a list

Comments

0

You should be able to do this fairly easily with a Counter:

$ python3
Python 3.4.2 (default, Oct  8 2014, 10:45:20) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
@>>> from collections import Counter
@>>> c = Counter(['quick', 'brown', 'fox', 'jumps', 'lazy', 'dog'])
@>>> total = sum(c.values())
@>>> result = dict()
@>>> for key, value in c.items():
@...   result[key] = value/total
@... 
@>>> result
{'dog': 0.16666666666666666, 'quick': 0.16666666666666666, 'fox': 0.16666666666666666, 'brown': 0.16666666666666666, 'jumps': 0.16666666666666666, 'lazy': 0.16666666666666666}

or, to make it super pythonic:

dict([ (key, value/total) for key,value in c.items() ])

2 Comments

it probably doesn't help that list.map is not a function; you are presumably looking for the built-in function map
Alternatively, use a list comprehension; much, much more readable and pythonic than lambdas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.