-
Notifications
You must be signed in to change notification settings - Fork 183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pylogix - compare a previously acquired tag list with the current one #235
Comments
Hi @horus61 I've formatted your code properly. A few comments, don't use Here's some history on a similar topic #230 prev_ints = run_script()
while True:
y = run_script()
print(y)
# compare lists here
# if different, update prev_ints
# do something
# else if equal, don't do anything
sleep(3) Luckily for us we have the Zen of Python https://peps.python.org/pep-0020/
And we have 30,000 ways to do a list comparison lol https://www.digitalocean.com/community/tutorials/how-to-compare-two-lists-in-python I would probably use list comprehension for your use case since we do care about the order of the items in the list. l1 = [10, 20, 30, 40, 50]
l2 = [50, 75, 30, 20, 40]
res = [x for x in l1 + l2 if x not in l1 or x not in l2]
print(res)
if not res:
print("Lists l1 and l2 are equal")
else:
print("Lists l1 and l2 are not equal") If you need to know when an individual index change, then you could use zip: prev_ints = run_script()
did_change = False
while True:
y = run_script()
print(y)
for val_y, val_p in zip(y, prev_ints):
if val_y != val_p:
print(f"The values {val_y} {val_p} aren't equal")
did_change = True
if did_change:
prev_ints = y
did_change = False
sleep(3) Here's a few videos I would watch on these topics: |
Thank you Fernando for your help and I bother you again for further advice. I would like to identify the elements of the list that have changed since the previous reading according to their index because I would like to create an alarm report. |
You're welcome, your English is great! If you take the previous example that compares each value, you just have to create a new list, no need to get the index. Take a look below. # Initialization vars before loop is started
prev_ints = run_script()
did_change = False
changed_items = [] # <- New list
while True:
y = run_script()
print(y)
# Loop through all values and compare them
for val_y, val_p in zip(y, prev_ints):
# Compare new value with prev value
if val_y != val_p:
changed_items.append(val_y) # <- Add item to list
print(f"The values {val_y} {val_p} aren't equal")
did_change = True
if did_change:
prev_ints = y
did_change = False
# send report logic here # <- New line
sleep(3) |
HI. Thanks for the advice you give me. I wanted to show you the raspberry pi application that I would like to create to monitor the variables on the plc to integrate an alarm message with the sending of sms. This is an example with a variable simulator. I'm a newbie and it certainly doesn't look nice and you would do it more professionally. |
@horus61 I recommend you check out our discord, someone there might be able to further guide you. The best advice I can give you is to start small. Build each step of what you are trying to accomplish one by one on their own and then mash them together in the end. Also if you seek help in the discord your might want to translate function names and variables to English, otherwise is hard to follow the code. For example:
|
Good morning everyone. I state that I am new to python programming and I would like to ask you for advice on how to compare a previously acquired tag list with the current one to check for any changes in the values. Thanks for your availability.
I used this code
The text was updated successfully, but these errors were encountered: