简体   繁体   English

我在使用“范围内”的Python列表显示时遇到问题

[英]I am having trouble with a Python list display using “in range”

My first question here, so if I don't do something correctly, please tell me so I can correct it and/or do it correctly next time. 我的第一个问题是,如果我没有正确地做某事,请告诉我,以便我可以纠正它和/或下次正确地做。

I am trying to allow a user to input ten numbers, then spit them back out in the reverse order they gave them. 我试图允许用户输入十个数字,然后按照他们给出的相反顺序将它们吐出来。 I can't get the ranges right, though, because it keeps either asking me for just 9 numbers, or it doesn't assign anything to the last variable in my list. 但是,我不能正确地获得范围,因为它不仅要求我提供9个数字,要么它不会为我列表中的最后一个变量分配任何内容。

I am using Python 3.x. 我使用的是Python 3.x.

Here is my code: 这是我的代码:

#Creating my list
myNumbers=[1,2,3,4,5,6,7,8,9,10,11]

#Creating a for loop for inputs
for A in range (1,10):
    myNumbers[A]=input("Enter a number: ")


#Creating a for loop for outputs
for B in range (10,1,-1):
    print(myNumbers[B])

It's only allowing me to input 9 numbers, and then my output is the number 11, then my reverse input. 它只允许我输入9个数字,然后我的输出是数字11,然后我的反向输入。

Any help would be much appreciated. 任何帮助将非常感激。

range always omits the last value, and starts from 0 by default (remember that lists are 0-indexed in Python, so a 10-element list has indices 0 through 9). range始终省略最后一个值,默认情况下从0开始(请记住,列表在Python中为0索引,因此10个元素的列表具有索引0到9)。

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

Also, instead of using range(10,1,-1) , I would recommend just using reversed : 另外,我建议只使用reversed ,而不是使用range(10,1,-1)

for i in reversed(range(10)): # iterates from 9 down to 0

or, since you are just printing out the items in the list, just iterate over the reversed list items directly: 或者,由于您只是打印出列表中的项目,只需直接遍历反向列表项:

for item in reversed(myNumbers):
    print(item)

The reason it's only letting you enter 9 numbers, is that the first element in a list has index of 0 , so you should be assigning to A[0]...A[9] 它只允许你输入9个数字的原因是列表中的第一个元素的索引为0 ,所以你应该分配给A[0]...A[9]

If you start with an empty list, you can append to it as many times as you wish, so you don't need to know the size in advance. 如果您从一个空列表开始,您可以根据需要多次append它,因此您无需事先知道大小。

my_numbers = []

for i in range(10):
    my_numbers.append(input("Enter a number: "))

for item in reversed(my_numbers):
    print(item)

Loop variables in Python don't have to be numbers, you can iterate over anything that returns a sequence of objects. Python中的循环变量不必是数字,您可以迭代返回一系列对象的任何东西。

There is a shorthand way of creating a list like this called a list comprehension. 有一种创建像这样称为列表理解的列表的简便方法。 Then your program becomes just 3 lines 然后你的程序只有3行

my_numbers = [input("Enter a number: ") for i in range(10)]

for item in reversed(my_numbers):
    print(item)

Try using range (0,10) instead of range (1,10) 尝试使用范围(0,10)而不是范围(1,10)

range (1,10) is similar to for (i=1;i<10;i++) which runs only 9 times(does not run for i=10) also, the array index starts from 0 and not 1 范围(1,10)类似于for (i=1;i<10;i++) ,它只运行9次(不运行i = 10),数组索引从0开始而不是1

print( '\n'.join(reversed([input("Enter a number: ") for i in range(10)])) )

or 要么

li = []
for i in xrange(4):
    li.insert(0,input("Enter a number: ")
print( '\n'.join(li) )

This solution doesn't create a new (reversed) list object. 此解决方案不会创建新的(反向)列表对象。 We directly construct the desired list. 我们直接构建所需的列表。
It can be shortened to 它可以缩短为

li = []
any(li.insert(0,input("Enter a number: ")) for i in xrange(4))
print( '\n'.join(li) )

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

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