range()

range() 可以取一个范围内的数,返回一个列表

In [4]: range(10)        # 表示取10个数
Out[4]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [5]: range(0,10)      # 表示从0取到10(不包括10)
Out[5]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [6]: range(0,10,2)    # 每隔2个取一次
Out[6]: [0, 2, 4, 6, 8]

In [7]: range(0,10,3)    # 每隔3个取一次
Out[7]: [0, 3, 6, 9]

xrange() 用于与 range() 一致,range() 会直接打印出来,占用内存,而 xrange() 不会打印出来,只有遍历的时候才打印出来,因此不会占用内存

In [8]: range(10)
Out[8]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [9]: xrange(10)
Out[9]: xrange(10)
时间: 2024-08-29 12:46:47

range()的相关文章

range()用法

来源:http://www.cnblogs.com/wangwp/p/4535299.html 例子:http://www.cnblogs.com/hongten/p/hongten_python_range.html 函数原型:range(start, end, scan): 参数含义:start:计数从start开始.默认是从0开始.例如range(5)等价于range(0, 5); end:技术到end结束,但不包括end.例如:range(0, 5) 是[0, 1, 2, 3, 4]没有

Leetcode 34. Search for a Range

34. Search for a Range Total Accepted: 91570 Total Submissions: 308037 Difficulty: Medium Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm's runtime complexity must be in the order of O(l

Swift 中的Range和NSRange不同

Swift中的Ranges和Objective-C中的NSRange有很大的不同,我发现在处理Swift中Ranges相关的问题的时候,总是要花费比我想象的更多的时间.不过,现在回过头来看看,发现Swift中的Ranges的使用还是比较合理的,但是想要正确的使用Ranges真的需要一些特别的技巧. 看一个例子,下面这段代码展示的是截取以指定的字符开头和以指定的字符结尾的子字符串: ? 1 2 3 4 5 6 var str = "Hello, playground"   let ran

swift -- 定义空字符串 hasPrefix hasSuffix trim split join range

// 定义空的字符串 var str1 = "" var str2 = String() str1.isEmpty      // 判断字符串是否为空 // 输出字符串中所有的字符 var str3 = "As god name" for c in str3{ println(c) } Int.max   // Int类型的最大值 Int.min   // Int类型的最小值 var arr1 = ["c", "oc", &q

qvalue: Check that you have valid p-values or use a different range of lambda

ERROR: The estimated pi0 <= 0. Check that you have valid p-values or use a different range of lambda. 重现错误的代码: ps <- runif(3e5)library(qvalue)ps <- ps[ps < 0.75]qs <- qvalue(ps) Error in pi0est(p, ...) :  ERROR: The estimated pi0 <= 0. C

SAP程序代码中RANGE表的用法注意点

前几天写了个程序,在读SQL代码的时候,选择条件 in 一张range table,结果导致程序DUMP,SAP的LOG如下: 错误原因:RANGE表当用于WHERE条件是,只限较小的数据量的情况(约2000条左右): 若为大数据量应该用FOR ALL ENTRIES IN的语法,或者其它方式来改写.否则会DUMP

[LeetCode]Count of Range Sum

题目:Count of Range Sum Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j (i ≤ j), inclusive. Note:A naive algorithm

python练习-for range if continue

for i in range (1,6):        print         print         print "i=", i,         print "hello,how",         if i==3:             continue         print 'are you today?'

python练习-for range if bread

for i in range (1,6):        print        print        print "i=", i,        print "hello,how",        if i==3:             break        print 'are you today?' #当i=3的时候,程序会跳出

一道题目- Find the smallest range that includes at least one number from each of the k lists

You have k lists of sorted integers. Find the smallest range that includes at least one number from each of the k lists. For example, List 1: [4, 10, 15, 24, 26] List 2: [0, 9, 12, 20] List 3: [5, 18, 22, 30] The smallest range here would be [20, 24]