IUnknown
IUnknown

Reputation: 9839

Python - modifying items in a list

I have a list of instances of a class A

class A:
def __init__(self,ox,oy):
self.x=ox
self.y=oy

list1=[A(3,0),A(5,0),A(7,3),......]

Now I need to find out the instance in the list which has y' as non-zero - and apply that value to all the other members in the list.
It is given that only one unique member will have y as non-zero.
With the usual for-loop we would need to iterate the list twice - with or without comprehension.
Is there a way to achieve this any better.
I have not used filter and map much but feel there may be a better option.
Help is appreciated.

Upvotes: 0

Views: 79

Answers (2)

Joran Beasley
Joran Beasley

Reputation: 114098

import numpy

a = numpy.array([[3,0],[5,0],[7,3]])
zero_mask = a[:,1] == 0

a[zero_mask] = a[~zero_mask][0]

unfortunately it does not use your A class ...

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799520

No, there isn't. At least two loops would be required no matter how it was implemented.

Upvotes: 1

Related Questions