请大家帮忙看看,为什么运用了副本,原来的列表也空了
def transform_messages(messasges,sent_messages):
‘’’发送全部信息’’’
while messages:
sending_message=messages.pop()
print(f’sending message:{sending_message}’)
sent_messages.append(sending_message)
def send_messages(messages,sent_messages):
‘’’打印未发送信息和已发送信息两个列表’’’
print(‘Here are the original messages’)
for message in messages:
print(message)
print(‘Here are the sent messages’)
for sent_message in sent_messages:
print(sent_message)
messages=[‘A’,’B’,”C”,”D”,”E”,”F”]
sent_messages=[]
transform_messages(messages[:],sent_messages)
send_messages(messages,sent_messages)
sending message:F
sending message:E
sending message:D
sending message:C
sending message:B
sending message:A
Here are the original messages
Here are the sent messages
F
E
D
C
B
A
关于 LearnKu
List variable passed by address, not by value. You list variable will be changed at the same time when you change the content of it in anywhere.
Try using new varible for it, for example