将修改后的列表重置为iPython上的先前值

时间:2018-03-26 09:26:25

标签: python

我希望使用切片在{@ 1}}中删除'(', ')'

'[', ']'

用切片方法替换

    # the raw data 
    square = ['(', ')', '.', '^', '-']
    # the result I want
    square = ['[', ']', '.', '^', '-']

我必须在修改过的广场上进行操作,如:

    In [45]: square[:1] = ['[', ']']
    In [46]: square
    Out[46]: ['[', ']', ']', '.', '^', '-']
  # sliced with wrong index, it should be [:2]

或手动重新指定为方格

 square.remove(']')

在此过程中,如何将修改后的 In [47]: square = ['(', ')', '.', '^', '-'] ...: square[:2] = ['[', ']'] In [48]: square Out[48]: ['[', ']', '.', '^', '-'] 重置为未修改状态?

1 个答案:

答案 0 :(得分:2)

我会使用list()

从旧列表中创建一个新列表
square = ['(', ')', '.', '^', '-']
backup_square = list(square)
# Here I take the 1st value from square list and I put different data in it
square[0] = "DIFFERENT VALUE"

然后,您将能够访问backup_square中以前版本的square

print backup_square
>>> ['(', ')', '.', '^', '-']