zoukankan      html  css  js  c++  java
  • python 2 range, list, and set

    这里主要说的是用python中的range来模拟for循环

    转载请声明本文的引用出处:仰望大牛的小清新

    1.range(var1,var2,var3): range产生一个列表(list),var1<=其中元素<var2,即左闭右开的区间,每个元素之间的间隔为var3,如果没有给var3,默认为1,并且var3 可以为负数,当var3所确定的方向和var1,var2之间的方向不一致时,返回空列表,具体请看下面的例子

    2.list:list是有序组织容纳元素的容,用[(left bracket and right bracket)]括起来,list本身可嵌套意味着list中的元素可以是另一个list。注意,python中list的下标从0开始

    一个简单的练习代码如下:

     1 # -*- coding: utf-8 -*-
     2 from __future__ import unicode_literals
     3 print "练习 list 以及 用range实现的 for 循环"
     4 
     5 the_count = [1, 2, 3, 4, 5]
     6 fruits = ['apples', 'oranges', 'pears', 'apricots']
     7 change = [1, 'pennies', 2, 'dimes', 3, 'quarters']
     8 
     9 # this first kind of for-loop goes through a list
    10 
    11 for number in the_count:
    12     print "This is count %d" % number
    13 
    14 # same as above
    15 for fruit in fruits:
    16     print "A fruit of type: %s" % fruit
    17 
    18 # also we can go through mixed lists too
    19 # notice we have to use %r since we don't know what's in it
    20 for i in change:
    21     print "I got %r" % i
    22 
    23 # we can also build lists, first start with an empty one
    24 elements = [] # empty list
    25 
    26 # then use the range function to do 0 to 5 counts
    27 for i in range(0, 6):
    28     print "Adding %d to the list." % i
    29     # append is a function that lists understand
    30     elements.append(i)
    31 
    32 # now we can print them out too
    33 for i in elements:
    34     print "Elements was: %d" % i
    View Code

    我们还可以将range的结果直接放入list中,并用%r调试输出

    1 # python 2
    2 elements = []
    3 elements.append(range(0,6))
    4 
    5 # now we can print them out too
    6 for i in elements:
    7     print "Elements was: %r" % i

    这样list 中只有一个元素,且为一个列表,输出如下

    我们还可以自己嵌套定义"多维"list,比如 a = [[1,2,3],["你好","嘻嘻"]]

     我们还可以使用remove(value)方法将第一次出现value的值删去,但是,要注意这里最好使用while循环,for循环删除似乎是隔一个删一个,背后原理暂不清楚,还是对python的底层了解太少,但是结果如下

    1 # numbers中的内容是: [0,1,2,3,4,5]
    2 for num in numbers:
    3     print "Delete number: ",num
    4     numbers.remove(num)
    5     print "now the number is: ",num
    6     print "Now the list 'numbers' is: ", numbers

    删除结果是:

    因此还是应该使用while循环,或者for range来删除所有元素,for in不可行

     3. 关于对象方法中的self这个参数:

    例如:使用我们有一个list 对象名为test,我们调用了test中的append(5)方法,我们只给了一个参数5,但是报错会给出如下信息

    raceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: test() takes exactly 1 argument (2 given)

    这里的第一个指的就是self,在真正执行时,会传入自身,即test,因此这里提到了 2 given

  • 相关阅读:
    NIO[读]、[写]在同一线程(单线程)中执行,让CPU使用率最大化,提高处理效率
    解码(ByteBuffer): CharsetDecoder.decode() 与 Charset.decode() 的不同
    ACCESS与SQL Server下SQL Like 查询的不同
    试着用c写了一个多线程的同步
    收藏:c语言的多线程同步
    java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] 不能使用 '(未知的)';文件已在使用中
    Linux进程管理
    MySQL数据备份
    MySQL索引
    Socket.IO 概述
  • 原文地址:https://www.cnblogs.com/luruiyuan/p/6249434.html
Copyright © 2011-2022 走看看