Python编程:从入门到实践读书笔记-8 函数

8.4 传递列表
       向函数传递列表(可能是名字、数字或更复杂的对象(如字典))会很有用。
将列表传递给函数后,函数就能直接访问其内容。
     情景:使用函数,访问用户列表,问候每个人。
     方式一:不适用函数的情况模拟
     eg:
          def greet_users(names):                    #形参names是一个列表
               for name in names:
                    msg = "Hello, " + name.title() + "!"
                    print(msg)
          usernames = ['chandler','joey','rose']
          greet_users(usernames)
     op:
          Hello, Chandler!
          Hello, Joey!
          Hello, Rose!

8.4.1 在函数中修改列表
     将列表传递给函数后,函数可修改列表,修改是永久性的,可高效处理大量数据。
     情景: 将需要打印的设计存储在一个列表中,打印后移到另一个列表。
     eg:
          unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
          completed_models = []
          while unprinted_designs:
               current_design = unprinted_designs.pop()
               print("Printing model: " + current_design)
               completed_models.append(current_design)
          print("\nThe following models have been printed:")
          for completed_model in completed_models:
               print(completed_model)
     op:
          Printing model: dodecahedron
          Printing model: robot pendant
          Printing model: iphone case

          The following models have been printed:
          dodecahedron
          robot pendant
          iphone case
     注意:
          1. 代码逻辑:
                    a. 定义未打印的设计列表以及需要转移至的一个空列表;
                    b. 打印‘未被打印的列表’的每个元素,并将其添加至空列表;
          2. 空列表、循环while及方法append的使用。
     方式二:使用函数模拟
     eg:
          def print_models(unprinted_designs,completed_models):
               while unprinted_designs:
                    current_design = unprinted_designs.pop()
                    print("Printing models: " + current_design)
                    completed_models.append(current_design)
          def show_completed_models(completed_models):
               print("\nThe following models have been printed:")
               for completed_model in completed_models:
                    print(completed_model)
          unprinted_designs = ['iphone case', 'robot pendant', 'dodecahedron']
          completed_models = []
          print_models(unprinted_designs,completed_models)
          show_completed_models(completed_models)
     op:
         Printing models: dodecahedron
         Printing models: robot pendant
         Printing models: iphone case

         The following models have been printed:
         dodecahedron
         robot pendant
         iphone case 
      注意:
          1. 代码逻辑:
                    a. 定义两个函数,其中函数print_models 的两个形参设置为未打印和已完成打印,
                        此函数功能是将列表互换,使用while循环;
                        函数show_completed_model的形参显示为已打印,功能是展示已经打印的元素,
                        使用for循环遍历列表;
                    b. 定义 unprinted_designs,completed_models两个列表,并调用函数。
          2. 空列表、循环while及方法pop、append的使用。
          3. 大部分工作代码在函数中,主程序较少,易理解、扩展及维护。
          4. 一个函数负责一项具体工作,优于所有工作在一个函数中完成。

8.4.2 禁止修改函数列表
     情景:在8.4.1情景中,打印所有设计后,依然保留未打印的设计作为备份。
     方案:可向函数传递列表的副本,而非原件。
     eg:
          function_name(list_name[:])
     切片表示法[:]创建列表副本,在在8.4.1情景中,调用函数print_models 时,创建未打印设计副本:
          print_models(unprinted_designs[:], completed_models)
     注意:
          虽然向函数传递列表的副本可保留原始列表的内容,但除非有充分的理由需要传递副本
     否则还是应该将原始列表传递给函数,因为让函数使用现成列表可避免花时间和内存创建副本
     从而提高效率,在处理大型列表时尤其如此

练习:
8.9
def show_magicians(magician_names):
    for magician_name in magician_names:
        print(magician_name.title())
magician_names = ['chandler','joey','rose']
show_magicians(magician_names)

8.10
方式1:
def show_magicians(magician_names):
    for magician_name in magician_names:
        new_magician_name = "The Great " + magician_name
        print(new_magician_name.title())
magician_names = ['chandler','joey','rose']
show_magicians(magician_names)
方式2:
def make_great(making_great,magician_names):
    while making_great:
        current_making_great = making_great.pop()
        current_magician_name = magician_names.pop()
        current_magician_name = current_making_great + ' ' + current_magician_name
        magician_names.insert(0,current_magician_name)
def show_magicians(magician_names):
    for magician_name in magician_names:
        print(magician_name.title())
making_great = ['The Great','The Great','The Great']
magician_names = ['chandler','joey','rose']
make_great(making_great,magician_names)
show_magicians(magician_names)
方式3:
def make_great(making_great,magician_names):
    while making_great:
        current_making_great = "The Great " + making_great.pop()
        magician_names.append(current_making_great)
def show_magicians(magician_names):
    for magician_name in magician_names:
        print(magician_name.title())
making_great = ['chandler','joey','rose']
magician_names = []
make_great(making_great,magician_names)
show_magicians(magician_names)
8.11
def make_great(making_great,magician_names):
    while making_great:
        current_making_great = "The Great " + making_great.pop()
        magician_names.append(current_making_great)
def show_magicians(magician_names):
    for magician_name in magician_names:
        print(magician_name.title())
making_great = ['chandler','joey','rose']
magician_names = []
make_great(making_great[:],magician_names)
show_magicians(magician_names)
print(making_great)
print(magician_names)
此题错在定义函数时,对参数就使用切片副本,应在调用函数时使用切片副本

          




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值