简体   繁体   English

如何实现一个函数来撤消修改列表的最后一个输入?

[英]How do I implement a function that undoes the last input that modified a list?

So I recently made a program that adds, removes (either just plain removes or by a user input [ie if an item is less than a user input then it gets deleted and the such]) and modifies packages (dictionaries containing beginning and end dates, destination and price) in a list所以我最近制作了一个添加、删除(或者只是简单的删除或者通过用户输入[即如果一个项目小于用户输入那么它被删除等等])和修改包(包含开始和结束日期的字典)的程序、目的地和价格)在列表中

When I implemented the remove/modify functions, I also made sure to append the original item to a new list so that when I had to call the undo function it'd just append the original item to the original list again, however, the function just deletes the item that was in the list after the remove/modify calls and just appends the item that got removed/modified当我实现删除/修改功能时,我还确保将原始项目附加到新列表,以便当我不得不调用撤消功能时,它只会再次将原始项目附加到原始列表,但是,该功能在 remove/modify 调用后删除列表中的项目,并附加被删除/修改的项目

What I tried to do (my train of thought) was that I wanted to create a new list called "lst" where I stock the items that were about to be removed, and I would essentially use it in the undo function (basically the function that you call undo list).我试图做的(我的思路)是我想创建一个名为“lst”的新列表,在其中存放将要删除的项目,并且我基本上会在撤消功能中使用它(基本上是功能您称之为撤消列表)。 In the undo function I enumerate thru l and lst, and I tried to append the item from the lst list.在撤消函数中,我通过 l 和 lst 进行枚举,并尝试从 lst 列表中附加该项目。 Now I see how I messed up given that the index will probably not be the same and I caught an IndexError so I resorted to popping the previous item现在我知道我是如何搞砸的,因为索引可能不一样,我发现了一个 IndexError 所以我诉诸于弹出前一项

the code: https://pastebin.com/taZAjhQf the function that I was talking about:代码: https : //pastebin.com/taZAjhQf我正在谈论的功能:

def undo(l,lst):
    for el,pachet in enumerate(l):
        l.append(lst[el])
        l.pop()

if you could at least give me a hint or tell me if I'm on the right track...如果你至少能给我一个提示或告诉我我是否在正确的轨道上......

My suggestion is to maintain a log of the operations that took place (similar to what accountants do with their bookkeeping).我的建议是保留所发生操作的日志(类似于会计师对簿记所做的工作)。 That way you're storing the operations that took place rather than the actual state.这样你就可以存储发生的操作而不是实际状态。 You can construct the final state by following your log and applying the operations that took place in order.您可以通过跟踪日志并应用按顺序发生的操作来构建最终状态。 For undoing, you can pop off the last operation and reconstruct the state (I'll leave it to you to think of how you might optimise that)对于撤消,您可以弹出最后一个操作并重建状态(我将留给您考虑如何优化它)

This technique is quite popular these days, especially in systems that make use of Kafka.这种技术如今非常流行,尤其是在使用 Kafka 的系统中。 For more ideas into this technique, you can check out this free ebook from Jay Kreps and there's also a nice article on it.有关此技术的更多想法,您可以查看 Jay Kreps 的这本免费电子书,还有一篇关于它的好文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我有一个追加到列表的输入,如何打印出最后一个列表项(最后一个输入)? - I have an input that appends to a list, how do I print out the last list-item(the last input)? 如何在 dict/set/list 中实现 lambda function - How do I implement a lambda function inside a dict/set/list 如何阅读包含特定字符串的最后修改文件? - How do I read the last modified file that contains a certain string? 如何获取上次在Python中修改文件的时间? - How do I get the time a file was last modified in Python? 如何将列表输入 function 并在 Python 中接收列表? - How do I input a list into a function and recieve a list in Python? 如何在此 function 上迭代输入列表,因此 - How do I iterate an input list over this function and consequently 如何实现修改后的交叉熵损失 function? - How to implement a modified cross entropy loss function? 如何在while循环中保存来自输入()function的多个结果,而不是仅在Python中的最后一个结果? - How do i save multiple results from a input() function in a while loop, not the last result only in Python? 如何使用“Nones last”对列表进行排序 - How do I sort a list with “Nones last” 如何获取列表的最后一个元素? - How do I get the last element of a list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM