I’m not quite getting there syntax-wise: I have a dropdown list component on a form whose data is bound to a SQL query that would populate it with several string items (‘names’) from a table.
When invoking this form at runtime, I need to select one of the items in this list/set based on a known column value (call it ‘OperatorName’) in a different table, and have that selected and shown.
In the scripting, Event Handlers on a property change for the dropdown list, I have been able select the first index with a event.source.setSelectedIndex(0)… But how do I iterate through the items in the list and select the one I need, finding the index for this ‘operator name’, rather than ‘0’, or the first? Thanks much!
Without seeing your code I’m making assumptions but if your string is in the second column of your dataset that your searching and your index is in the first then something like this would return the first index that matches the string your searching for.
ds = dataset_you_want_to_search
p = [x for x in ds if x == 'operator name']
Note that @bpreston’s code requires your data is already in the form of a pydataset. Also, a more efficient rewrite would be something like this:
ds = dataset_you_want_to_search
p = next(index for index, operator in ds if operator == 'operator name', None)
Using the next function makes iteration ‘lazy’, meaning that as soon as any match is found, a value is returned; using a square bracket list comprehension requires that the full list be traversed before any value can be returned.
Thanks much, but I’m not showing 2 columns in this particular dataset of the ‘Data’ property of the dropdown (UserName here is coming from the SQL column it is bound to). I’m getting this error, of course there isn’t an index column. But. Event.sourse.setSelectedIndex(0) with a ‘0’ still works…
Unpacking like this would require you have exactly two columns in your pydataset. Maybe too fragile to be generally useful, but does make a limited approach like this more readable.
I’m somewhat unclear on exactly what the flow of the logic is supposed to be here.
Right now, every time the data property is updated (by a query?), you’re running this additional query and trying to match up names.
What if there are multiple matches?
Could this be a JOIN operation at the DB level?
The specific error in your screenshot is just syntax; it wants you to change that line to something like this: p = next((index for index, username in ds if username == row["Operator1"]), 0)
But that will then fail with ‘not enough values to unpack’ because your dataset only has a single column, so try this: p = next((index for index, username in enumerate(ds) if username == row["Operator1"]), 0)
The enumerate builtin will “add” an index column to the iteration loop (effectively).
(I’m using ‘supervisor’ rather than operator in this example. I need to handle both). But this got me going down the right path.
“What if there are multiple matches”: There won’t be , that ‘name’ is a primary key in the table
“Could this be a JOIN”: It’s not, as to the binding on the data property. It is selecting just one column
As to the flow of logic, I had originally got this code / logic handling sample for this some time ago. This for setting the selected index on a dropdown list, when just setting it to the first index (‘0’) rather than saying