鱼C论坛

 找回密码
 立即注册
查看: 4718|回复: 5

range和list的区别?

[复制链接]
发表于 2017-5-26 11:23:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
学完列表后对range、list分不清,感觉他们很像,请各位大神指点
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-5-26 11:32:28 | 显示全部楼层
range()是依次取顺序的数值,常与for循环一起用,如for each in rang(0,10):  for循环执行10次,each取值依次是0~9

list()是把字符串转换为列表,如 b = '123456'   c = list(b)   c打印出来会是一个列表 :['1', '2', '3', '4', '5', '6']

12.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-26 15:52:51 | 显示全部楼层
range 一般是用来进行循环迭代用的,跟for 语句一起
list 是列表,不仅仅可以用来迭代
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-26 16:10:42 | 显示全部楼层
ButcherRabbit 发表于 2017-5-26 11:32
range()是依次取顺序的数值,常与for循环一起用,如for each in rang(0,10):  for循环执行10次,each取值依 ...

谢谢,明白多了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-26 16:11:15 | 显示全部楼层
gopythoner 发表于 2017-5-26 15:52
range 一般是用来进行循环迭代用的,跟for 语句一起
list 是列表,不仅仅可以用来迭代

谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-26 16:51:39 | 显示全部楼层
本帖最后由 shuofxz 于 2017-5-26 16:53 编辑

遇到不懂的内置函数,可以先看看python的帮助文档,里面都会把这个函数的说明、用法等等都讲的很清楚,而且一般还会有一些例子帮助你理解
class range(start, stop[, step])
The arguments to the range constructor must be integers (either built-in int or any object that implements the __index__ special method). If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. If step is zero, ValueError is raised.

For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[i] < stop.

For a negative step, the contents of the range are still determined by the formula r[i] = start + step*i, but the constraints are i >= 0 and r[i] > stop.

A range object will be empty if r[0] does not meet the value constraint. Ranges do support negative indices, but these are interpreted as indexing from the end of the sequence determined by the positive indices.

Ranges containing absolute values larger than sys.maxsize are permitted but some features (such as len()) may raise OverflowError.

class list([iterable])
Lists may be constructed in several ways:

Using a pair of square brackets to denote the empty list: []
Using square brackets, separating items with commas: [a], [a, b, c]
Using a list comprehension: [x for x in iterable]
Using the type constructor: list() or list(iterable)
The constructor builds a list whose items are the same and in the same order as iterable‘s items. iterable may be either a sequence, a container that supports iteration, or an iterator object. If iterable is already a list, a copy is made and returned, similar to iterable[:]. For example, list('abc') returns ['a', 'b', 'c'] and list( (1, 2, 3) ) returns [1, 2, 3]. If no argument is given, the constructor creates a new empty list, [].
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-8 06:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表