Skip to content

Commit 2b18093

Browse files
committed
Add map method for tupledict
1 parent 713dd3a commit 2b18093

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/pyoptinterface/_src/tupledict.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ def select(self, *keys, with_key=False):
104104
def clean(self):
105105
self.__select_cache = None
106106

107+
def map(self, func):
108+
return tupledict((k, func(v)) for k, v in self.items())
109+
107110

108111
def flatten_tuple(t):
109112
# (1, (2, 3), (4, 5)) -> (1, 2, 3, 4, 5)

tests/test_tupledict.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,15 @@ def test_tupledict_select():
5656
# Test select with key and value
5757
assert list(td.select(1, 2, with_key=True)) == [((1, 2), "a")]
5858
assert list(td.select(2, WILDCARD, with_key=True)) == [((2, 2), "c"), ((2, 3), "d")]
59+
60+
61+
def test_tupledict_map():
62+
td = tupledict([((i, i + 1), i) for i in range(10)])
63+
64+
td_m = td.map(lambda x: x**2)
65+
66+
assert isinstance(td_m, tupledict)
67+
68+
assert list(td_m.values()) == [i**2 for i in range(10)]
69+
70+
assert list(td_m.keys()) == list(td.keys())

0 commit comments

Comments
 (0)