[英]How to print only when something gets added into a list?
I got an idea where you have a list etc. of [1,2,3,4,5]
and my idea was that incase a number of those gets pop/deleted then it shouldn't print. 我知道您有
[1,2,3,4,5]
的列表等,我的想法是,如果其中一些被弹出/删除,则不应打印。 Let's say etc. we remove number 3
. 假设以此类推。我们删除了数字
3
。 In that case our list would be [1,2,4,5]
and the script should be usual. 在这种情况下,我们的列表将是
[1,2,4,5]
,脚本应该是常规的。 But whenever a value gets added to a list. 但是只要将值添加到列表中。 Then print out the whole list so etc. add number
6
– > [1,2,4,5,6]
- Print out the whole list. 然后打印出整个列表,以此类推。添加数字
6
–> [1,2,4,5,6]
-打印出整个列表。
The problem is that I don't want to get notified whenever there has been anything deleted so my idea from the beginning was to check the length of a list and then notify whenever it gets changed but then I realized that the wrong I am doing is that it going to notify whenever it gets added and/nor deleted which now why I am here. 问题是我不想在删除任何内容时收到通知,所以我的想法从一开始就是检查列表的长度,然后在列表更改时通知,但后来我意识到我做错了它会在添加和/或删除时通知,这就是为什么我在这里。
What I did was a compare of a new_name_list
vs old_name_list
but this one is going to notify for whatever change that would be happens basically. 我所做的是将
new_name_list
与old_name_list
进行比较,但这将通知基本上将发生的任何更改。
import names
def get_value(value):
value = names.get_full_name()
names_list = []
for names in names.get_last_name():
names_list.append(names)
break
identifier = ('{} {}').format(value, names_list)
return identifier
if __name__ == '__main__':
old_name_list = get_value()
while True:
new_name_list = get_value()
if new_name_list not in old_name_list:
print("Yay new name added")
else:
print('I will re try again in 5 sec')
time.sleep(5)
My question is - How can I make it so it print only whenever the value of names_list
will get notified ONLY when something gets added but not deleted? 我的问题是-如何使其仅在
names_list
的值仅在添加但未删除的情况下得到通知时才打印?
etc. 等等
1. [1,2,3,4,5] - print from beginning
2. [1,2,4,5] - Deleted 3 - Do not print
3. [1,2,4,5,6] - Print list, something got added
4. [1,4,5,6] - Deleted 2 - Do not print
5. .........
Algorithm could be the following: 算法可能如下:
Example: 例:
def detect_change(old_list, new_list):
changed_flag = False
old_list.sort()
new_list.sort()
if len(old_list) == len(new_list):
for i in range(0, len(old_list)):
if old_list[i] != new_list[i]:
changed_flag = True
elif len(old_list) < len(new_list):
changed_flag = True
return changed_flag
list1 = ["a", "b", "c", "d"]
list2 = ["a", "b", "c", "k"]
print(detect_change(list1, list2))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.