简体   繁体   中英

Why does this nested list comprehension generate an error?

conn_request, weights = zip(*[
    ((conn, request), request[2])
    for conn in unchoked_conns
    for request in conn.peer_requests
])

Generates:

    for conn in unchoked_conns
ValueError: need more than 0 values to unpack

I cannot work out what I'm doing wrong. I think it has some thing to do with the dependence of request on conn ?

>>> a, b = zip(*[])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 0 values to unpack

Your LC produces an empty list. Try verifying that unchoked_conns has any elements.

zipping an empty list returns a single empty list, hence the assignment fails. That is consistent with the zip help, which clearly says:

The returned list is truncated in length to the length of the shortest argument sequence

In your case, the shortest argument sequence has length 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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