Reputation: 103
In python, i would like to create several user-defined functions f(x) that are defined only at integer values x = i, where i ranges from 1 to several hundred. The functions themselves cannot be expressed as analytical or logical formulae but the values f(i) are known for all values i of the support range and so could be written as a table of pairs of values i, f(i).
Although i have written quite a few functions in python using def ..., I am still a python newbiwe, and i do not yet know how to efficiently code a function that must inherently use some form of numerical lookup table. I seek your assistance with an example of what would be appropriate code.
Upvotes: 0
Views: 1316
Reputation: 6190
Use a dictionary e.g.
LOOKUP = {
0: 100,
1: 101,
3: 102,
9: 103,
}
def f(x):
return LOOKUP[x]
Upvotes: 2
Reputation: 599788
You don't want a function for this. A mapping in Python is expressed via a dictionary.
mapping = {
1: 300,
3: 990,
45: 267,
# etc
}
result = mapping[value]
Upvotes: 2