Adrian_G
Adrian_G

Reputation: 163

Advanced python for loop understanding

I once copied a line of code from stackoverflow:

clusters = [x for n, x in enumerate(vecarray) if x not in vecarray[:n]]

I understand what it does, but not how. It gives me all the unique rows of the array 'vecarray' and saves them in 'clusters'. I do have basic understanding of normal for loops, but this structure looks different.

How would I edit this line of code, if I also want to save the indices the found rows (basically in what row did the loop find the next unique number)

Can someone shed light on it?

Upvotes: 1

Views: 66

Answers (2)

ggeop
ggeop

Reputation: 1375

This loop format in Python is called loop comprehensions, is an alternative and compact way to create lists in Python.

About your question you can keep the indices with multiple ways, it depends with what you want

Approach 1:

clusters = [{'idx':n, 'value':x} for n, x in enumerate(vecarray) if x not in vecarray[:n]]

Example:

#Dummy input
vecarray=[1,2,1,2,3,4]

clusters = [{'idx':n, 'value':x} for n, x in enumerate(vecarray) if x not in  vecarray[:n]]

print(clusters)

#Result
[{'idx': 0, 'value': 1}, {'idx': 1, 'value': 2}, {'idx': 4, 'value': 3},{'idx': 5, 'value': 4}]

Approach 2:

Replace list comprehension with dict comprehension

clusters = { str(n): x for n, x in enumerate(vecarray) if x not in vecarray[:n]}

Example:

#Dummy input
vecarray=[1,2,1,2,3,4]

clusters = {str(n): x for n, x in enumerate(vecarray) if x not in vecarray[:n]}

print(clusters)

#Result
{'0': 1, '1': 2, '4': 3, '5': 4}

Upvotes: 1

Nick
Nick

Reputation: 607

The enumerate() function is already returning you the row the value was found in as n.

If you simply change your code to:

clusters = [(n, x) for n, x in enumerate(vecarray) if x not in vecarray[:n]]

Then you should have a list of tuples, instead of a list of values.

You can then do:

for n,x in clusters:
    print(x," found at row ",n)

Your line of code is a list comprehension. You can re-write it to be a standard for loop instead:

clusters=[]
for n,x in enumerate(vecarray):
    if x not in vecarray[:n]:
        clusters.append((n,x))

Upvotes: 2

Related Questions