python - list comprehension without assignment

2019-06-16 02:11发布

Today I was parsing a directory index with a list of paths to zip files using BeautifulSoup and came across an interesting thing. Let's assume I would like to take all the href properties of the tags I got and put them directly into a Queue:

q = Queue.Queue()
[q.put(tag['href']) for tag in soup.findAll('a')]

I've never run into a situation like this before where a comprehension could be used inline without assigning it to anything, just to generated another iterator through some routine call. Is this considered bad practice? Is it "pythonic", per se? Was there a better one-liner to put all the items into the queue?

4条回答
地球回转人心会变
2楼-- · 2019-06-16 02:23

If you think of it as a loop over the list returned by soup.findAll it would look like this:

for tag in soup.findAll('a'):
    q.put(tag['href'])

This is probably the more 'pythonic' form as 'explicit is better than implict'

查看更多
Fickle 薄情
3楼-- · 2019-06-16 02:39

There are many opinions on this thread, I can only speak from coding conventions at my organization.

there are many ways to affect a loop, but a key attribute of list comprehensions is that they create lists, with one item for each in the iterated over sequence.

>>> import Queue
>>> q = Queue.Queue()
>>> [q.put(item) for item in range(5)]
[None, None, None, None, None]
>>>

this unused list is obviously wasteful. As such, this construction, a list comprehension with unused return value; is forbidden from appearing in our code base. An explicit loop like above, or a generated combined with something that consumes it, for instance:

>>> any(q.put(item) for item in xrange(5))
False
>>>

or just:

>>> for item in xrange(5):
...     q.put(item)
...
>>>

is required to pass review.

查看更多
别忘想泡老子
4楼-- · 2019-06-16 02:43

Probably not a better one-liner, but I'd (personally) consider doing this instead of:

for tag in soup.findAll('a'):
    q.put(tag['href'])

to be bad practice. Firstly, the one liner will generate a list full of [None], which is likely more inefficient. Secondly, it's not immediately clear what the code is doing; seeing a list comprehension generally implies the resultant list will be used in some way.

查看更多
欢心
5楼-- · 2019-06-16 02:46

This has been asked many times, e.g., here and here. But it's an interesting question, though. List comprehensions are meant to be used for something else.

Other options include

  1. use map() - basically the same as your sample
  2. use filter() - if your function returns None, you will get an empty list
  3. Just a plain for-loop

while the plain loop is the preferable way to do it. It is semantically correct in this case, all other ways, including list comprehension, abuse concepts for their side-effect.

In Python 3.x, map() and filter() are generators and thus do nothing until you iterate over them. So we'd need, e.g., a list(map(...)), which makes it even worse.

查看更多
登录 后发表回答