zoukankan      html  css  js  c++  java
  • Learn Python the hard way, ex39 列表的操作

     1 #!/usr/bin/python
     2 #coding:utf-8
     3 
     4 ten_things = "apples oranges crows telephone light sugar"
     5 print "wait there's not 10 things in that list,let's fix that"
     6 
     7 stuff = ten_things.split(' ')  #10个东西,以' '间隔开
     8 more_stuff = ["day","night","song","frisbee","corn","banana","girl","boy"]  #更多的原料
     9 
    10 while len(stuff) != 10:  #取得List stuff的长度,当不等于10时循环
    11     next_one = more_stuff.pop()    #截取more_stuff的最后一个元素(默认值)
    12     print "adding:",next_one    #输出pop取得的值
    13     stuff.append(next_one)     #使用append将之前截取的值赋予list:stuff
    14     print "there's %d items now" % len(stuff)
    15 
    16 print "there we go : ",stuff
    17 
    18 print "let's do some things with stuff"
    19 
    20 print stuff[1]  #第二个元素,list从0计数
    21 print stuff[-1]  #whoa!fancy   最后一个元素(-1)
    22 print stuff.pop()
    23 print ' '.join(stuff)  #what?cool!
    24 print '#'.join(stuff[3:5])  # super stellar

    Output:

     1 wait there's not 10 things in that list,let's fix that
     2 adding: boy
     3 there's 7 items now
     4 adding: girl
     5 there's 8 items now
     6 adding: banana
     7 there's 9 items now
     8 adding: corn
     9 there's 10 items now
    10 there we go :  ['apples', 'oranges', 'crows', 'telephone', 'light', 'sugar', 'boy', 'girl', 'banana', 'corn']
    11 let's do some things with stuff
    12 oranges
    13 corn
    14 corn
    15 apples oranges crows telephone light sugar boy girl banana
    16 telephone#light

    join()

    加分题的一些参考: Python 面向对象编程 函数式编程

  • 相关阅读:
    tomcat解压版环境变量配置
    十天冲刺(第二阶段)
    十天冲刺(第二阶段)
    十天冲刺
    十天冲刺第二阶段()
    十天冲刺(第二阶段)
    十天冲刺(第二阶段)
    十天冲刺(第一阶段)
    十天冲刺(第一阶段)
    十天冲刺(第一阶段)
  • 原文地址:https://www.cnblogs.com/sub2020/p/7865363.html
Copyright © 2011-2022 走看看