python prohibit implementation of a function to modify the list

Following small for everyone to bring a python ban function to change the implementation method of the list. Xiao Bian feel very good, now for everyone to share, but also to be a reference. Xiao Bian together to follow up to see it
sometimes, need to disable the function to modify the list. For example, to modify the operation of fission, but also to retain the original design list of unprinted, for the record. To solve this problem, the list can be passed to a function rather than a copy of the original; any changes made affect only the function of such a copy, but does not affect the original

function_name(list_name[:])

8-9 magician magician: Creates a list containing the name of the magician, and passes it to a function called show_magicians (), this function prints the list the name of each magician. 8-10 great magician great magician: as you complete the exercise

8-9 Write a function named make_great () function to modify the list of magician, a magician in the name of each of the words were added to both "the Great". Call the function show_magicians (), the list of confirmed magician has indeed changed.

8-11 unchanged unchanged magician magician: to complete the exercise and modify your programs written 8-10, when you call the function make_great (), passing it a copy of the magician list. Did not want to modify the original list, please return the modified list, and store it to another list. Both lists were used to call show_magicians (), confirm a list containing the name of the original magician, and the other contains the list is the addition of the words "the Great" magician name.

def make_great(magicians,new_magicians): #对列表修改的函数
 while magicians:
   current_magician = magicians.pop() #删除原列表中的元素
   current_magician = "The Great " + current_magician
   new_magicians.append(current_magician)
 
def show_magicians(new_magicians):
 for magician in new_magicians:
  #便利所有的magicians中的元素
  print(magician) 
 
magicians = ['fake','ppd','moon']
new_magicians = []
 
make_great(magicians[:],new_magicians)#调用函数make_great 传递magicians[]副表magicians[:]
show_magicians(new_magicians)#输出新表
show_magicians(magicians)#输入原表

Results of the:

The Great moon
The Great ppd
The Great fake
fake
ppd
moon

I write to you, for everyone to recommend a very wide python learning resource gathering, click to enter , there is a senior programmer before learning to share

Experience, study notes, there is a chance of business experience, and for everyone to carefully organize a python to combat zero-based item of information,

Python day to you on the latest technology, prospects, learning small details that need to comment on
the above python ban this function is to modify the list of methods to achieve small series to share the entire contents of all of the

发布了5 篇原创文章 · 获赞 0 · 访问量 908

Guess you like

Origin blog.csdn.net/haoxun10/article/details/104663462