簡體   English   中英

python列表計數元素

[英]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 / spark。 這可能是導致我在update1中遇到問題的原因嗎?

應對方案肯定會更好。 您對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 import的舊版本的python 2,則可以使用以下變通辦法強制進行float划分。

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

根據我的要求,您可以做類似的事情

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)

話雖這么說,問題不是很清楚,因為您提到列表type(),但顯示列表中單詞頻率的百分比

您應該可以使用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}

或者,使其成為超級pythonic:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM