简体   繁体   English

没有in的python列表理解

[英]python list comprehension without in

if flattend is just a list of strings, for example 例如,如果flattend只是一个字符串列表

['There','is','only','passion','and','piece','is','a','lie','lie','lie']

then in following two lines 然后在以下两行中

c = Counter(flattened)
vocab = [x for x, count in c.items() if count>=2]

what does the part [x for x,...] mean? [x代表x,...]是什么意思? also, shouldn't count be of type tuple as i suppose it is a counter item? 同时,不应该算作是类型元组的,我想这是一个计数器项目? how come this part count>=2 work?! 这个零件count>=2怎么起作用?

Note: I understand from debugging that the first line converts the list into a counter and the second one removes the items that occurred less than twice. 注意:我从调试中了解到,第一行将列表转换为计数器,第二行将发生少于两次的项目删除。 but i can't really interpret the syntax 但是我真的不能解释语法

So the syntax here is a little confusing, but what's actually happening is that each item in c.items() is a tuple containing a word and its count. 因此,这里的语法有些混乱,但是实际上发生的是c.items()中的每个项目都是一个包含一个单词及其数量的元组。

A more clear way of writing this would be: 一种更清晰的书写方式是:

vocab = [x for (x, count) in c.items() if x>=2]

but it could be also be done like this: 但也可以这样完成:

vocab = [x[0] for x in c.items() if x[1]>=2]

where x is a tuple. 其中x是一个元组。

It can also be helpful to look at what c actually looks like. 查看c实际外观也可能会有所帮助。 If you print c, you see: 如果打印c,则会看到:

>>> print c
Counter({'lie': 3, 'is': 2, 'and': 1, 'a': 1, 'There': 1, 'only': 1, 'passion': 1, 'piece': 1})

and c.items() c.items()

>>> print c.items()
[('and', 1), ('a', 1), ('lie', 3), ('is', 2), ('There', 1), ('only', 1), ('passion', 1), ('piece', 1)]

Counter will return a dictionary like structure. 计数器将返回类似结构的字典。 So you need to iterate over keys and values, key is x and value is count. 因此,您需要遍历键和值,键是x,值是count。 If we look closely at c.items() 如果我们仔细观察c.items()

c.items() #list of tuples with (key,value)

[('and', 1),
 ('a', 1),
 ('lie', 3),
 ('is', 2), # x->'is' ,count->2
 ('There', 1),
 ('only', 1),
 ('passion', 1),
 ('piece', 1)]

So if you are iterating this list for a single tuple there are two components: a word and associated count. 因此,如果要为一个元组迭代此列表,则有两个部分:单词和相关计数。 For count you are checking if the count>=2 if yes then returning that key which in list comphrension is x 对于计数,您要检查count>=2是否为是,然后返回列表含义为x的键

[x for x, ...] is just using x as an variable while iterating over some array... [x for x, ...]只是在迭代某个数组时使用x作为变量...

x, count captures the two items that serve as iterated values from c.items() . x, countc.items()捕获用作迭代值的两个项目。

If you were to print the results of: for _ in c.items(): print(_) That would print out a list of tuples like (x, count) . 如果要for _ in c.items(): print(_)的结果: for _ in c.items(): print(_) ,将打印出一个元组列表,如(x, count)

[x for x, count in c.items() if count > 2] just preserves x in the array while using the count iterable as a filter. [x for x, count in c.items() if count > 2]只是将x保留在数组中,同时使用可迭代的count作为过滤器。

Let's break it down into lines: 让我们将其分解为几行:

vocab = [           # line0
         x          # line1
         for        # line2
         x, count   # line3
         in
         c.items()
         if
         count>=2]  # line7

Each tuple from c.items() is composed of a key, x , (the thing that was counted) and a count (the number of times that key was seen). 来自c.items()每个tuple都由一个键x (被计数的事物)和一个count (被看到该键的次数)组成。

On each loop, you can imagine the next tuple is pulled, then unpacked, so that instead of needing to use a single value with indices 0 and 1 , you can just refer to them by name; 在每个循环中,您可以想象下一个tuple被拉出,然后解压缩,这样您无需使用索引为01的单个值,而只需按名称引用即可; anontuple[0] becomes x , anontuple[1] becomes count . anontuple[0]变成xanontuple[1]变成count

The count>=2 line then filters the results; 然后count>=2行过滤结果。 if count is less than 2 , we stop processing this item, and pull the next one. 如果count小于2 ,我们将停止处理此项目,并拉出下一个项目。

The plain x on the far left is the item to produce; 最左边的纯x是要生成的项目; when the filtering check is passed, we shove the corresponding x into the resulting list unmodified. 通过过滤检查后,我们会将相应的x保留到未修改的结果list

Converting to a regular loop, it would look like this (lines matched to listcomp lines): 转换为常规循环,如下所示(与listcomp行匹配的行):

vocab = []                  # line0
for x, count in c.items():  # lines 2-5
    if count >= 2:          # lines 6-7
        vocab.append(x)     # line1

If unpacking is confusing to you, you could instead imagine it as: 如果解压缩对您造成困扰,则可以将其想象为:

vocab = []              # line0
for item in c.items():  # lines 2, 4 and 5
    x = item[0]         # line3
    count = item[1]     # line3
    if count >= 2:      # line 6-7
        vocab.append(x) # line1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM