zoukankan      html  css  js  c++  java
  • 3、列表 list

    列表
    >>> list=['aaa','bbb','ccc']
    >>> print list
    ['aaa', 'bbb', 'ccc']
    >>> print list[1]                    -- list从0开始计数
    bbb
    >>> print list[-1]                    --输出最后一个
    ccc
    >>> print list[1:3]                --  输出一段列表(顾头不顾尾)
    ['bbb', 'ccc']
    >>> list.append('ddd')        -- append添加一个值
    >>> print list
    ['aaa', 'bbb', 'ccc', 'ddd']
    >>> list.pop()                        -- pop删除最后一个值
    'ddd'
    >>> print list
    ['aaa', 'bbb', 'ccc']
    >>> list.pop(2)                        -- 删除某一个位置的值
    'ccc'
    >>> print list
    ['aaa', 'bbb']
    >>> list.count('aaa')                -- count列表中有几个aaa
    1
    >>> print len(list)                    --计算列表长度
    3
    >>> list.index('bbb')                -- index 输出bbb在列表中第几个,没有会报错
    1
    >>> list[1]                                -- 第一个确实是bbb
    'bbb'
    >>> list                
    ['aaa', 'bbb', 'ccc']
    >>> list.insert(2,'bbb2')                --  在2号数前插入一个
    >>> list
    ['aaa', 'bbb', 'bbb2', 'ccc']
    >>> list[2]='bbb3'                        --  修改一个元素的值
    >>> list
    ['aaa', 'bbb', 'bbb3', 'ccc']
    >>> del list                                    -- 不用删除列表,以释放内存
    >>> test=['a','b','c']                            --  用循环打印数组
    >>> for i in test:
    ...     print i   ,
    ... 
    a b c
    >>> print range(5)                        -- 得到一个递增列表
    [0, 1, 2, 3, 4]
    >>> range(3,8)                            -- 得到一段的递增列表
    [3, 4, 5, 6, 7]
    >>> list=['a']*10                            -- 得到重复的列表
    >>> list
    ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a', 'a']
     
    元组
    元组是阉割版的列表,一旦生成不能修改
    list=('aaa','bbb','ccc')
    只有count index函数
     
    练习:
    输入工资,列出商品,判断是否能买列出的商品,如果能则判断还剩多少工资,并循环
    如果不能,输出还需要几个月可以买。
    1. #!/usr/bin/python
      salary=int(raw_input('Input your salary:'))
      salary_hist=salary
      whileTrue:
              shop_list=['car 200000','ipad 4800','coffee 32','Mac 8888','house 2000000']
              for product in shop_list:
                      name =product.split()[0]
                      price=int(product.split()[1])
                      id=shop_list.index(product)
                      print 'you can buy: ',id,' ',name,'  ',price
              max_id=shop_list.index(shop_list[-1])
              want_id=raw_input('Input the ID you want buy:')
      ###############    strong the id input    ######################
              while want_id =='' or int(want_id) not in range(0,int(max_id)):
                      print 'You max input id between 0~%s'%max_id
                      want_id=raw_input('Input the ID you want buy:')
              want_id=int(want_id)
      ################################################################
              name=shop_list[want_id].split()[0]
              price=int(shop_list[want_id].split()[1])
              if price<=salary:
                      salary=salary-price
                      print 'You have a %s,now you have %s money'%(name,salary)
              else:
                      print 'You have no money,you need to work %s month'%(price/salary_hist)
                      break
  • 相关阅读:
    javaTemplates-学习笔记三
    索引
    WTForms
    session权限限制
    vue-cli脚手架项目中组件的使用
    vue补充
    表单输入绑定
    vue指令系统介绍
    vue-cli脚手架安装和webpack-simple模板项目生成
    rest-framework之视图
  • 原文地址:https://www.cnblogs.com/kissdb/p/4009569.html
Copyright © 2011-2022 走看看