Python 数字序列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17175809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:40:44  来源:igfitidea点击:

Python Sequence of Numbers

pythonlistsequence

提问by xtheking

I've decided not to waste my summer and start learning python. I figured I'd start learning looping techniques so I wanted to start with a basic list of numbers, aka, write a for loop that will generate the numbers 1 - 10.

我决定不浪费我的暑假开始学习 Python。我想我会开始学习循环技术,所以我想从一个基本的数字列表开始,也就是写一个 for 循环来生成数字 1 - 10。

This is what I have:

这就是我所拥有的:

def generateNumber(num):
    i=0 
for i in range(num):
    return i
return i

and the code doesn't work. I want to get an output in a list like this:

并且代码不起作用。我想在这样的列表中得到一个输出:

>>> generateNumber(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

采纳答案by tom10

Trying to be consistent with what you first tried, you could do something like this

试图与你第一次尝试的保持一致,你可以做这样的事情

def generateNumber(num):
    mylist = []
    for i in range(num+1):
         mylist.append(i)
    return mylist


x = generateNumber(10)

but, you could, instead just say,

但是,你可以,而只是说,

x = range(10+1)  # gives a generator that will make a list

or

或者

x = list(range(10+1))  # if you want a real list

In general though, you should keep this list based on inputting the number 10 so it is [0...9] and not [0...10].

不过,一般而言,您应该根据输入数字 10 保留此列表,因此它是 [0...9] 而不是 [0...10]。

回答by TankorSmash

The rangefunction already does what you are setting out to do.

range功能已经完成了您要做的事情。

If you're in Python 2, range(10)returns 0 through 9, or in Python 3 it's list(range(10)).

如果你在 Python 2 中,range(10)返回 0 到 9,或者在 Python 3 中它是list(range(10)).

回答by pkacprzak

By default, range(n) produces list of numbers [0, 1, ..., n-1].

默认情况下,range(n) 生成数字列表 [0, 1, ..., n-1]。

If you want a list of numbers from a to b inclusive, you should call:

如果你想要一个从 a 到 b 的数字列表,你应该调用:

range(a, b + 1)

Which in your case is:

在你的情况下是:

range(1, 11)

回答by Steven Wexler

You probably want to create a list in your function, use the yieldkeyword, or use the built in listfunction.

您可能希望在您的函数中创建一个列表,使用yield关键字,或使用内置的列表函数。

def generateNumberList(num):
    myList = []
    for i in range(num):
        myList.append(i)
    #Notice that your return the list you've created rather 
    #than each individaul integer
    return myList

print generateNumberList(10)

def generateNumberList2(num):
    for i in range(10):
        yield i

for i in generateNumberList2(10):
    print i

def generateNumberList3(num):
    return list(range(num))

print generateNumberList3(10)

回答by codeprogress

It might help to implement this with the ability to specify a range:

通过指定范围的能力来实现这一点可能会有所帮助:

def generateNumber(low, high):
    '''returns a list with integers between low and high inclusive
    example: generateNumber(2,10) --> [2,3,4,5,6,7,8,9,10]
    '''
    return range(low, high+1)

This can also be done with the built-in range function:

这也可以通过内置的 range 函数来完成:

range(10)   --> [0,1,2,3,4,5,6,7,8,9]   #note the "off by one"
range(11)   --> [0,1,2,3,4,5,6,7,8,9,10]
range(2,11) --> [2,3,4,5,6,7,8,9,10]

More about range: http://docs.python.org/2/library/functions.html#range

更多关于范围:http: //docs.python.org/2/library/functions.html#range

回答by Sabrina

Easiest way to generate numbers is by using rangefunction

生成数字的最简单方法是使用range函数

The range()function has two sets of parameters, as follows:

range()函数有两组参数,如下:

range(stop)

range(stop)

> stop: Number of integers (whole numbers) to generate, starting from
> zero. eg. range(3) == [0, 1, 2].

range([start], stop[, step])

range([start], stop[, step])

> start: Starting number of the sequence.
> stop: Generate numbers up to, but not including this number.
> step: Difference between each number in the sequence.

Note that:

注意:

  • All parameters must be integers.
  • All parameters can be positive or negative.
  • range() (and Python in general) is 0-index based, meaning list indexes start at 0, not 1. eg. The syntax to access the first element of a list is mylist[0]. Therefore the last integer generated by range() is up to, but not including, stop. For example range(0, 5) generates integers from 0 up to, but not including, 5.
  • 所有参数必须是整数。
  • 所有参数都可以是正数或负数。
  • range()(以及一般的 Python)是基于 0 索引的,这意味着列表索引从 0 开始,而不是 1。例如。访问列表第一个元素的语法是 mylist[0]。因此 range() 生成的最后一个整数最多但不包括停止。例如 range(0, 5) 生成从 0 到但不包括 5 的整数。

e.g.

例如

Print 0 to 9

打印 0 到 9

[user@linux ~]~ python -c "print list(range(10))" 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[user@linux ~]~ 

Print 1 to 9

打印 1 到 9

[user@linux ~]~ python -c "print list(range(1,10))"
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[user@linux ~]~ 

Print 1 to 10

打印 1 到 10

[user@linux ~]~ python -c "print list(range(1,11))"
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[user@linux ~]~ 

Reference: https://www.pythoncentral.io/pythons-range-function-explained/

参考:https: //www.pythoncentral.io/pythons-range-function-explained/