2016-02-26 67 views
0

我有一个代码如下python list counting elements

我怎样才能找到abc是一个由列表组成的列表?

我的地图功能有什么问题?

我想我的函数返回我的输入列表中每个元素的计数除以我的列表的长度。

喜欢的东西

{'brown': 0.16666666666666666, 'lazy': 0.16666666666666666, 'jumps': 0.16666666666666666, 'fox': 0.16666666666666666, 'dog': 0.16666666666666666, 'quick': 0.16666666666666666} 

我的代码:

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 ... } 

_______________________________________

更新1

我如下更新我的代码。我得到结果[('brown', 0), ('lazy', 0), ('jumps', 0), ('fox', 0), ('dog', 0), ('quick', 0)]任何想法为什么?如果我做return return list(map(lambda input: (input[0], input[1]), abc)),它给出正确的结果 - [('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) 

更新2

我使用pyspark /火花。这可能是我在update1中遇到的问题的原因吗?

+0

'是列表组成的列表?在abc上做一个for循环,然后用'type()'检查每个元素。如果他们都列出,那么你得到你想要的。 – GLHF

+1

['map'](https://docs.python.org/3/library/functions.html#map)是一个内建函数,不是方法,所以'abc.map'它不起作用,你必须使用'map(function,abc)' – Copperfield

回答

1

柜台解决方案肯定会更好。您使用tokens.count会给代码提供二次时间复杂度。继承人你的代码修复了。您应该注意,map是一个独立的函数,不是列表或任何其他类型的成员函数。

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) 

要测试,如果你有一个列表的列表,你可以使用isinstance检查父对象的类型,如果它是一个列表,并在它至少一个元素,可以遍历他们使用isinstance检查每个子对象是否是一个列表。

请注意,我让你的函数返回一个元组列表,这意味着这些项目是只读的,但是你可以通过改变行来使它返回一个列表列表。

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

如果你仔细看看它,你会看到一组圆括号代替了方括号,使每个元素成为一个列表。

如果您有不支持导入from __future__ import division的python 2的旧版本,则可以使用以下解决方法强制执行浮点除法。

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

我试过你的方法。我得到了一个答案:'('brown',0),('lazy',0),('jumps',0),('fox',0),('dog',0) ,0)]'当我使用'list(map(lambda input:(input [0],int(input [1])/ len(tokens)),abc))''。我的abc是[['brown',1],['lazy',1],['jumps',1],['fox',a],['dog',1],['quick', 1]]' – user2543622

+0

我正在浏览MOOC和pyspark。不确定蟒蛇版本! – user2543622

+1

使用'float(input [1])'强制浮点除法。 –

0

根据我想你问你可以不喜欢

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) 

话虽这么说,这个问题不是很清楚,因为你提的列表类型(),但显示词频的百分比在列表中

+0

任何想法为什么我的地图功能不起作用? – user2543622

0

你应该能够用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} 

,或者使其超Python的:

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

任何想法为什么我的地图功能不工作? – user2543622

+0

它可能不会帮助'list.map'不是一个函数;你大概是在寻找[内置函数图](https://docs.python.org/2/library/functions.html#map) – Hamms

+0

或者,使用[list comprehension](https:// docs。 python.org/2/tutorial/datastructures.html#list-comprehensions);比lambda更具可读性和pythonic。 – Hamms