一长串数字 [英] long list of numbers
问题描述
我想用python写一长串数字,但是我不想一个接一个地输入数字,因为那很无聊.那么,我该怎么做呢?对于那些熟悉MATLAB的人,我想写一些看起来像1:100的女巫,但我不知道如何用python编写! 顺便说一句,我可以在一个循环中为一个元素列表附加数字,但我正在寻找的东西大都类似于内置运算符.
I want to write a long list of numbers in python, but I don't want to enter the numbers one by one because that's boring. So, how can I do that? For the ones who are familiar with MATLAB, I want to write something witch looks 1:100, but I don't know how to write it in python! By the way, I can append numbers for a one-element-list in a loop but I am looking for something mostly like a built in operator.
推荐答案
是否需要类似内置的range
函数?
Do you want something like the builtin range
function?
>>> range(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,10,2)
[1, 3, 5, 7, 9]
在python 3.x中的
,range
不再返回列表,而是返回range object
.范围对象在很多方面的行为都类似于列表,但实际上并不是列表.如果您确实需要python 3.x上的列表,则可以使用list(range(...))
in python 3.x, range
doesn't return a list anymore, it returns a range object
. Range objects behave like lists in a lot of ways, but they aren't actually lists. If you really need a list on python 3.x, you can use list(range(...))
Numpy具有类似的功能arange
,它也可以与浮点数一起使用:
Numpy has a similar function arange
which will work with floats as well:
>>> np.arange(1,2,.1)
array([ 1. , 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9])
这篇关于一长串数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!