Replace integer values with another values

This is not Ignition directly question, but more Python question. And there are many Python gurus in this community…

What I want is to replace integer value with another value.
I have an integer which can have one of 4 values: 1,2,4,8 and I need to replace those with 0,1,2,3.
I know can do this with regular IF:

	if postaja1 == 1:
		postaja1 = 0
	elif postaja1 == 2:
		postaja1 = 1
	elif postaja1 == 4:
		postaja1 = 2
	elif postaja1 == 8:
		postaja1 = 3

but I’m almost confident that there is a better/shorter/nicer looking way…?

{1: 0, 2: 1, 4: 2, 8: 3}.get(postaja, -1) is the easiest option; you can keep the dictionary elsewhere if you might need to extend it later. There might be some bit-shifty-hack that I can’t immediately think of that covers this simple case, but extending it might be more difficult.

2 Likes
translations = {
	1: 1,
	2: 3,
	5: 8,
	13: 21
}

value = translations.get(3, value) # some default value

print value

edit: dang @PGriffith beat me

2 Likes

I knew it…:+1:
Thank you!