1

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?

1
  • Please update the question with some sample data, the result of clusters and what you would like to see changed. Commented Feb 21, 2020 at 14:52

2 Answers 2

2

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))
Sign up to request clarification or add additional context in comments.

Comments

1

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}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.