user1253798
user1253798

Reputation: 13

Python - What do these two lines do?

Being new to Python, I'm having a heck of a time figuring out what these two lines do:

for i in [j for j in xrange(0, N) if [k for k in xrange(j) if now[k] == now[j]] == []]:
   for j in [k for k in xrange(1, K + 1) if [l for l in xrange(i) if now[l] == k] == []]:

Is there a way to rewrite this so noobs like myself could understand it? Thanks.

Upvotes: 1

Views: 270

Answers (4)

Niklas B.
Niklas B.

Reputation: 95298

I'm almost positive that this code is equivalent to:

for i in range(0, N):
  prefix = set(now[:i])
  if now[i] in prefix: continue

  for j in range(1, K + 1):
    if j in prefix: continue

    # do something

or (which would be a bit less efficient, but nicely reflects the idea behind the code):

for i, j in itertools.product(range(0, N), range(1, K + 1)):
  prefix = now[:i]
  if now[i] not in prefix and j not in prefix:
    # do something with i and j

BUT it's written in a convulted, inefficient way (especially the [...] == [] is nasty and pointless). Maybe by someone who feels pretty clever using complex structures where simple ones would be much better suited.

Upvotes: 3

Harshith J.V.
Harshith J.V.

Reputation: 887

You are probably confused with list comprehension. Its difficult even for me to write list comprehension. But its not difficult to interpret whats already written list comprehension script means. Since you said you want noob-friendly code, the below code might be something like new-user friendly. (The execution of the code might be slightly different, the code is presented for understanding purpose only)

#first list comprehension expanded ...
first_list = []
for j in xrange(0, N):
    inner_first_list = []
    for k in xrange(j):
        if now[k] == now[j]:
            inner_first_list.append(k)
    if inner_first_list == []:
        first_list.append(j)

#second list comprehension expanded ...
second_list = []
for k in xrange(1, K + 1):
    inner_second_list = []
    for l in xrange(i):
        if now[l] == k:
            inner_second_list.append(l)
    if inner_second_list == []:
        second_list.append(k)

#the actual loop ...
for i in first_list:
    for j in second_list:
        #your loop statement

You will get better understanding of list comprehension over here: Why are Python lambdas useful?

Upvotes: 0

Taymon
Taymon

Reputation: 25666

Ugh. Reading that was physically painful. This is an excellent example of how not to use list comprehensions.

Here's a strict rewriting that doesn't use list comprehensions:

for i in xrange(0, N):
    implicit_list_1 = []
    for k in xrange(i):
        if now[k] == now[i]:
            implicit_list_1.append(i)
    if implicit_list_1 == []:
        for j in xrange(1, K + 1):
            implicit_list_2 = []
            for l in xrange(i):
                if now[l] == j:
                    implicit_list_2.append(l)
            if implicit_list_2 == []:

And here's a more idiomatic rewriting:

for i in xrange(N):
    if now[i] not in now[:i]:
        for j in xrange(1, K + 1):
            if j not in now[:i]:

This is assuming that K is a separate variable, and not a mistyped k.

Also, those one-letter variable names are poor style. Better to use variable names that actually reflect the use of the variable.

Upvotes: 4

rob05c
rob05c

Reputation: 1233

I suspect what's confusing you are the nested list comprehensions.

To begin with, you could pull them out into variables

firstlist = [j for j in xrange(0, N) if [k for k in xrange(j) if now[k] == now[j]] == []]
for i in firstlist:
   secondlist = [k for k in xrange(1, K + 1) if [l for l in xrange(i) if now[l] == k] == []]
   for j in secondlist:

You could further make it more readable by changing the nested list comprehensions to construct the lists entirely with ordinary for loops.

You should definitely check out that documentation link to try to grasp list comprehensions. They're powerful constructs, and in my opinion more readable for those who understand them. But nested list comprehensions can be rather difficult to sort out.

Upvotes: 0

Related Questions