简体   繁体   English

Python:生成数字1-10的列表,但是1最终出现在列表的末尾吗?

[英]Python: Generating list of numbers 1-10, but 1 ends up at the end of the list?

I have just started learning how to program using Python, and have been going through some exercises to help me improve. 我刚刚开始学习如何使用Python进行编程,并且已经通过一些练习来帮助我进行改进。 For one of the exercises, I have to program a Sieve of Eratosthenes, and as part of doing this, I wanted to generate a list of numbers 1 through n . 对于其中的一项练习,我必须编写一个Eratosthenes筛子的程序,而在此过程中,我想生成一个数字1到n的列表。

My code is as follows: 我的代码如下:

def primelist(n): #returns a list of all primes lower than or equal to n
    grid=[]
    k=1
    while k in range (1, n+1):
        grid.insert(-1, k)
        k+=1
    return grid

This is as far as I got, because when I tested what I had written so far with: 就我所知,这是因为当我测试到目前为止所写的内容时:

print(primelist(10))

my code output the list [2, 3, 4, 5, 6, 7, 8, 9, 10, 1] and I can't figure out why it is doing this. 我的代码输出了列表[2、3、4、5、6、7、8、9、10、1],但我不知道为什么这样做。 Any explanation would be much appreciated! 任何解释将不胜感激!

In Python, list.insert(index, item) actually inserts item BEFORE index . 在Python中, list.insert(index, item)实际上插入item在BEFORE index之前。

So list.insert(-1, k) inserts k before -1. 因此list.insert(-1, k)在-1之前插入k Hence your results. 因此,您的结果。

To do what you want to do properly, the following two lines are equivalent 为了正确执行您想做的事情,以下两行是等效的

list.insert(len(list), k)
list.append(k)

Both methods modify the actual list (instead of returning a new list), as expected. 两种方法都按预期修改了实际列表(而不是返回新列表)。

See more here: https://mail.python.org/pipermail/tutor/2010-January/073754.html 在此处查看更多信息: https : //mail.python.org/pipermail/tutor/2010-January/073754.html

insert method for list inserts the element at the index before the first argument. list的insert方法将元素插入到第一个参数之前的索引处。 -1 is the index of last element. -1是最后一个元素的索引。 So, when you are inserting at -1 , you are always inserting just before the last element. 因此,当您插入-1 ,您总是在最后一个元素之前插入。 For example you have a list a as: 例如你有一个清单a为:

a = [1,2,3,4]
print a[-1]  # It will print 4

So, when you are trying to insert first element (1) in your list using grid.insert(-1, k) , it goes to last (or first as it will be only element), as there is no other element. 因此,当您尝试使用grid.insert(-1, k) )在列表中插入第一个元素(1)时,由于没有其他元素,它会移至最后一个(或第一个元素,因为它将是唯一的元素)。 All insert operations after first element add the elements to the second last position in the list. 在第一个元素之后的所有插入操作会将元素添加到列表中的倒数第二个位置。

This leads you see only 1 at last position while all other elements are in order as you expect. 这导致您在最后一个位置仅看到1 ,而所有其他元素都按预期排列。

As already mentioned by @coln, you can either use list.append(k) or list.insert(len(list), k) to insert/append/add elements to the end of the list. 如@coln所述,您可以使用list.append(k)list.insert(len(list), k)在列表的末尾插入/添加/添加元素。

I don't know why it's backwards, but your code is not the way to add elements to a list. 我不知道为什么它会倒退,但是您的代码不是将元素添加到列表的方式。 Also you don't use the variable n in your code (probably a typo), so I will just add the function to generate a list through 1 to n . 另外,您不会在代码中使用变量n (可能是拼写错误),因此我将仅添加函数以生成从1到n的列表。

def primelist(n):
    grid=[]
    for k in range(1,n+1):
        grid.append(k)
    return grid

This code will return [1,2,3,4,5,6,7,8,9,10] as you requested. 此代码将根据您的请求返回[1,2,3,4,5,6,7,8,9,10]

xrange can do this for you, remember to give xrange a end value that is one more than the one you want. xrange可以为您完成此操作,请记住为xrange提供一个比您想要的值大一的最终值。

So to generate numbers from 1 to n : xrange(1,n+1) 因此要生成从1到n的数字:xrange(1,n + 1)

The example below will give you a list from 1 to 10. 下面的示例将为您提供1到10的列表。

>>> primelist=list(xrange(1,11))
>>> primelist
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM