2016-02-26 88 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