在 Python 中使用查找表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50508262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Look Up Tables in Python
提问by Vishvachi Sinha
I am using python to automate a piezoelectric droplet generator. For each value of a pulse length, a suitable value of voltage will be there to produce a signal to give away a droplet. This value of voltage keeps changing in every run(for example, + or -10). So I have a database of different value of voltages for every pulse length.
我正在使用 python 来自动化压电液滴发生器。对于脉冲长度的每个值,都会有一个合适的电压值来产生信号以释放液滴。此电压值在每次运行中都在不断变化(例如,+ 或 -10)。所以我有一个针对每个脉冲长度的不同电压值的数据库。
I would like to know some things about using lookup tables in python. For my task, I want to pick a random pulse length from 15 to 70, and associate this value with a particular range of voltages from the database (for example: for a value 17, I would like the program to access the lookup table and return a range of voltages 35-50). Is it possible to take the entire range and not just a single value. Since, I am new to coding and python, I am not really sure. Any help is welcome. Thank you.
我想知道一些关于在 python 中使用查找表的事情。对于我的任务,我想选择一个从 15 到 70 的随机脉冲长度,并将该值与数据库中特定范围的电压相关联(例如:对于值 17,我希望程序访问查找表和返回电压范围 35-50)。是否可以采用整个范围而不仅仅是单个值。因为,我是编码和 python 的新手,我不太确定。欢迎任何帮助。谢谢你。
回答by offeltoffel
Since we are not given any further information about what ranges should be associated with which values, I assume you will transfer my answer to your own problem.
由于我们没有得到关于哪些范围应该与哪些值相关联的任何进一步信息,我假设您会将我的答案转移到您自己的问题上。
Look-up-Tables are called dictionary
in python. They are indicated by curly brackets.
查找表dictionary
在 python中调用。它们用大括号表示。
Easy example:
简单的例子:
myDict = {1: [1, 2, 3, 4, 5],
2: [2, 3, 4, 5, 6],
3: [3, 4, 5, 6, 7]}
Here you create a dictionary with three entries: 1, 2, 3. Each of these entries has a range associated with it. In the example it is of logic range(i, i+5)
.
在这里,您创建了一个包含三个条目的字典:1、2、3。这些条目中的每一个都有一个与之关联的范围。在这个例子中它是逻辑的range(i, i+5)
。
You inquire your "Look-Up-Table" just like a list:
您可以像列表一样查询“查找表”:
print(myDict[2])
>>> [2, 3, 4, 5, 6]
(Note how [2]
is not index #2, but actually the value 2
you were looking for)
(注意如何[2]
不是索引 #2,而是2
您正在寻找的值)
Often you do not want to create a dictionary by hand, but rather want to construct it automatically. You can e.g. combine two lists of the same length to a dictionary, by using dict
with zip
:
通常,您不想手动创建字典,而是想自动构建它。例如,您可以使用dict
with将两个相同长度的列表组合到一个字典中zip
:
indices = range(15, 76) # your values from 15 to 75
i_ranges = [range(i, i+5) for i in indices] # this constructs your ranges
myDict = dict(zip(indices, i_ranges)) # zip together the lists and make a dict from it
print(myDict[20])
>>> [20, 21, 22, 23, 24]
By the way, you are not restricted to integers and lists. You can also go like:
顺便说一下,您不仅限于整数和列表。你也可以这样:
myFruits = {'apples': 20, 'cherries': 50, 'bananas': 23}
print(myFruits['cherries'])
>>> 50
回答by Mael Abgrall
numpy is your way to go, if you can put your database in a big array (and if your database is not too big)
numpy 是你要走的路,如果你可以把你的数据库放在一个大数组中(如果你的数据库不是太大)
so a quick example:
一个简单的例子:
import numpy
my_array = numpy.zeros([3, 8], dtype=numpy.uint8)
this will output the following array:
这将输出以下数组:
array([[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.]])
from there you can access the array with the following line:
从那里您可以使用以下行访问数组:
my_array[0]
it will output the first line:
它将输出第一行:
array([ 0., 0., 0., 0., 0., 0., 0., 0.])
you can do the same with columns:
你可以对列做同样的事情:
my_array[:, 0]
it will output the first column:
它将输出第一列:
array([ 0., 0., 0.])
using a dictionnary is also a good way.
使用字典也是一个好方法。
However, numpy is written in C and is way faster than python integrated functions. And it offers a number of useful features like mean, standard deviation, where (check where a value exist in the array)
但是,numpy 是用 C 编写的,比 python 集成函数要快得多。它提供了许多有用的功能,如均值、标准差、其中(检查数组中存在值的位置)
You can also create much more complicated array with numpy and explore them with ease
您还可以使用 numpy 创建更复杂的数组并轻松探索它们
回答by Aykut Kllic
Following is a linear-interpolating lookup implementation:
以下是线性插值查找实现:
from bisect import bisect_left
def lookup(x, xs, ys):
if x <= xs[0]: return ys[0]
if x >= xs[-1]: return ys[-1]
i = bisect_left(xs, x)
k = (x - xs[i-1])/(xs[i] - xs[i-1])
y = k*(ys[i]-ys[i-1]) + ys[i-1]
return y
For testing:
用于检测:
xs = [1, 2, 4, 8, 16, 32, 64, 128, 256]
ys = [0, 1, 2, 3, 4, 5, 6, 7, 8]
i_xs = [i/1000-500 for i in range(1000000)]
start_time = time.time()
ys = [lookup(x, xs, ys) for x in i_xs]
print("%s secs" % (time.time() - start_time))
I get around 1.8secs.
我得到大约 1.8 秒。