Lookup or find function in script

I started with a dataset with about 8000 rows of xy data. After a lot of calculations I now have a resulting dataset of about 36 x-axis values representing the locations of peaks and valleys of the original dataset.

What I want to do is take each x-axis value and find the cooresponding y-axis value from the original dataset.

I am doing all of this in scripting. How do I search for a value in a dataset or py dataset?


Unfortunately, you will have to loop through the original dataset looking for the value. There is no special scripting function for that.

Would using a dictionary help? I haven’t played with them myself, but I seem to remember reading about lookups in dictionaries being faster than iterating through lists…

Well, yes but you would have to iterate through the dataset to create the dictionary. So either way you have to perform the loop.

Yes, but if he’s already iterated through it once to get his “36 x-axis values”, why not create the dictionary at the same time?

You are absolutely right. I missed that. A dictionary does allow faster searching. Here is an example of using a dictionary:[code]dict = {}
dict[“key1”] = [arrayVal1, arrayVal2, …]
dict[“key2”] = [arrayVal1, arrayVal2, …]

print dict.has_key(“key1”)
print dict.get(“key1”, defaultvalue) # default value can be empty list or None[/code]

1 Like

I have been without my computer for about 3 weeks and I was given another project to work on. I am not very familiar with dictionaries either. This is what is working for me.

def findYvalue(ds1, ds2): import system pyPVData = system.dataset.toPyDataSet(ds1) pyRollData = system.dataset.toPyDataSet(ds2) headers = ["distance", "height"] newData = [] for xPV in pyPVData: for xRoll in pyRollData: if xPV["distance"] == xRoll["distance"]: newData.append([xRoll["distance"], xRoll["height"]]) dsGaugeBands = system.dataset.toDataSet(headers, newData) return dsGaugeBands

Thank you,