简体   繁体   English

仅在将某些内容添加到列表时如何打印?

[英]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_listold_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: 算法可能如下:

  1. Order old and new lists. 订购新旧清单。
  2. If length is equal -> compare element-to-element. 如果长度相等->比较元素与元素。 If new list contains different elements than old list, means new values were added. 如果新列表包含的元素与旧列表不同,则意味着添加了新值。
  3. If new list length > old list length, also means, that new elements were added. 如果新列表长度>旧列表长度,也意味着已添加新元素。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM