理解列表理解 [英] Understanding list comprehension

查看:140
本文介绍了理解列表理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程有点新鲜感。我创建了一个在其初始化程序中使用列表理解的类。如下所示:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'其中存储
字典,其中键是单词,值是它们的出现'''

def __init __(self:'Collection_of_words',file_name:str) - > None:
'''这个初始化器会读取文件中的单词
,并将它们存储在self.counts中'
l_words = open(file_name).read()。 ($)
s_words = set(l_words)

self.counts = dict([[word,l_words.count(word)]
for word
in s_words] )

我觉得我对新手来说没问题。有用!但我不完全明白这是如何在for循环中表示的。我的猜测是错的:

  self.counts = [] 
for s_words中的单词:
self .counts = [word,l_words.count(word)]
dict(self.counts)


解决方案

你的猜测根本没有错;你只是忘记追加并分配回 self.counts

 计数= [] 
在s_words中的单词:
counts.append([word,l_words.count(word)])
self.counts = dict(counts)

这就是列表理解所要做的事情。



您也可以将其翻译为字典理解:

<$ p $
$ self.counts = {word:l_words.count(word)for word in s_words}

或更好,请使用 collections.Counter() object 并保存所有这些工作:

  from collections import Counter 

def __init __(self:'Collection_of_words',file_name:str) - >无:
'''这个初始化程序会读取文件
中的单词,并将它们存储在self.counts的
中,并以open(file_name)作为infile:
self.counts = Counter(infile.read().split())



Counter()对象可以更有效地计算您的单词,并为您提供其他有用的功能,例如列出最前N个计数和合并计数的能力。


I'm sort of new to programming. I have created a class that uses list comprehension in its initializer. It is as follows:

class Collection_of_word_counts():
 '''this class has one instance variable, called counts which stores a 
dictionary where the keys are words and the values are their occurences'''

def __init__(self:'Collection_of_words', file_name: str) -> None:
    '''  this initializer will read in the words from the file,
    and store them in self.counts'''
    l_words = open(file_name).read().split()
    s_words = set(l_words)

    self.counts = dict([ [word, l_words.count(word)] 
                        for word 
                        in s_words])

I think I did alright for a novice. It works! But I don't exactly understand how this would be represented in a for-loop. My guess was terribly wrong:

self.counts =[] 
for word in s_words:
    self.counts = [word, l_words.count(word)]
dict(self.counts)

解决方案

Your guess was not wrong at all; you just forgot to append and assign back to self.counts:

counts = [] 
for word in s_words:
    counts.append([word, l_words.count(word)])
self.counts = dict(counts)

That's what a list comprehension does, essentially; build a list from the loop expression.

You could also translate that to a dictionary comprehension instead:

self.counts = {word: l_words.count(word) for word in s_words}

or better still, use a collections.Counter() object and save yourself all that work:

from collections import Counter

def __init__(self:'Collection_of_words', file_name: str) -> None:
    '''  this initializer will read in the words from the file,
    and store them in self.counts'''
    with open(file_name) as infile:
        self.counts = Counter(infile.read().split())

The Counter() object goes about counting your words a little more efficiently, and gives you additional helpful functionality such as listing the top N counts and the ability to merge counts.

这篇关于理解列表理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆