简体   繁体   English

如何在python中将列表返回到其原始状态?

[英]how to return a list to its original state in python?

every time a user inputs a message rotorI.append(rotorI.pop(0)) executes on the edited list, i want it to execute on the original list.每次用户输入消息rotorI.append(rotorI.pop(0))在编辑列表上执行时,我希望它在原始列表上执行。

rotorI = ['E', 'K', 'M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T','O', 'W','Y','H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J']

while True:
      msg = input("Enter a msg: ")
      for i in range(len(msg)):
          rotorI.append(rotorI.pop(0))

      print(rotorI)

I want the output to be:我希望输出是:

Enter a msg: hi
['M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T', 'O', 'W', 'Y', 'H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J', 'E', 'K']
Enter a msg: hi
['M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T', 'O', 'W', 'Y', 'H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J', 'E', 'K']

however this the output i receive:然而这是我收到的输出:

Enter a msg: hi
['M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T', 'O', 'W', 'Y', 'H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J', 'E', 'K']
Enter a msg: hi
['L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T', 'O', 'W', 'Y', 'H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J', 'E', 'K', 'M', 'F']

Make a copy of the list at each iteration在每次迭代时制作列表的副本

while True:
    msg = input("Enter a msg: ")
    newRotor = list(rotorI)
    for i in range(len(msg)):
        newRotor.append(newRotor.pop(0))
    print(newRotor)

Using collections.deque and string for displaying使用collections.deque和 string 进行显示

from collections import deque

rotorI = "EKMFLGDQVZNTOWYHXUSPAIBRCJ"
while True:
    msg = input("Enter a msg: ")
    newRotor = deque(rotorI)
    newRotor.rotate(-len(msg))
    print("".join(newRotor))

Enter a msg: hi
MFLGDQVZNTOWYHXUSPAIBRCJEK
Enter a msg: hih
FLGDQVZNTOWYHXUSPAIBRCJEKM
Enter a msg: hihi
LGDQVZNTOWYHXUSPAIBRCJEKMF
Enter a msg: hi
MFLGDQVZNTOWYHXUSPAIBRCJEK

Use copy of your list with using copy() function:使用 copy copy()函数使用列表copy()

your_main_list = ['E', 'K', 'M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T','O', 'W','Y','H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J']
while True:
      msg = input("Enter a msg: ")
      list_for_edit = your_main_list.copy()
      for i in range(len(msg)):
          list_for_edit.append(list_for_edit.pop(0))

      print(list_for_edit)

Output is gonna be :输出将是:

Enter a msg: hi
['M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T', 'O', 'W', 'Y', 
'H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J', 'E', 'K']
Enter a msg: hi
['M', 'F', 'L', 'G', 'D', 'Q', 'V', 'Z', 'N', 'T', 'O', 'W', 'Y', 
'H', 'X', 'U', 'S', 'P', 'A', 'I', 'B', 'R', 'C', 'J', 'E', 'K']
Enter a msg: 

暂无
暂无

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

相关问题 如何将反向列表返回到 python 中的原始 state - How to return a reversed list back to its original state in python 脚本完成后如何让键盘恢复原来的state? - How to return the keyboard to its original state after the completion of the script? 想知道如何在更改之前返回带有原始元组的列表 - wondering how to return a list with original tuple before its changed 如何在python中将unicode转换为其原始字符 - How to convert unicode to its original character in Python Python Pandas将列值更改为NULL并返回其原始值 - Python Pandas change column values to NULL and return to its original values Python:如何仅使用while循环返回列表而不修改原始列表? - Python: How to return a list without modifying the original list using a while loop only? Python,以其原始形式对列表进行排序 - Python, sorting my list from its original form Python随着原始变量的更改,列表中的附加元素也会更改 - Python The appended element in the list changes as its original variable changes 如何在保留 Python 中元素的原始顺序的同时返回字符串中的项目列表 - How to return a list of items in a string while preserving the original order of elements in Python Python:如何在句子的单词列表中找到一个字母并以原始大小写返回这些单词(大写/小写) - Python: How to find a letter in a sentence's list of words and return those words in their original case (upper/lower)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM