My datastruture is actually a dictionary that has a integer key and a value that is a list of tuples. I want to find a particular index in that list. For example:
d = {}
d[2] = [(1,-2),(2,4),(3,2)]
d[1] = [(1,-2),(1,4)]
I want to find all tuples in a list with index 2 or -2 (they can be in either first or second member of the tuple). Then based on certain conditions I want to remove tuple elements from the list. For example, I want to remove (1,-2) from the list d[2]. I did not find an efficient way to do this step because I also want to remove (1,-2) from the list d[1] whenever I remove it from d[2].
>>> a = [(1,1), (2,2)]
>>> if (1,1) in a:
... print 'Ok'
...
Ok
>>>
To remove stuff:
>>> b = [(1,-1,(3,1),(5,2)]
>>> filter(lambda a: a != (1,-1), b)
Should give you:
[(3,1), (5,2)]
So..
d = {}
d[2] = [(1,-2),(2,4),(3,2)]
d[1] = [(1,-2),(1,4)]
for k, v in d.items():
d[k] = filter(lambda a: a != (1,-1), v)
This will remove (1, -1) from your dictionary all entirely.
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.