[英]Storing items in a list for python
I am new to python and would like to ask a simple question.我是 python 新手,想问一个简单的问题。
I am trying to get this program to loop and every time it loops, it should store a variable in a list.我试图让这个程序循环,每次循环时,它都应该在列表中存储一个变量。
Please help请帮忙
This is my code so far:到目前为止,这是我的代码:
import random
for x in range(1,100):
q=random.randint(0,99)
w=random.randint(0,99)
e=q*w
q=[]#This is the list
print '%s * %s = %s'%(q,w,e)
You need to define the list outside of the loop, and append to it within the loop.您需要在循环外定义列表,并在循环内附加到它。
import random
# Define your list
l = []
for x in range(1,100):
q=random.randint(0,99)
w=random.randint(0,99)
e=q*w
# Append the result to your list
l.append(e)
print '%s * %s = %s'%(q,w,e)
print 'The list becomes: %s' % l
Let's go through your code line-for-line让我们逐行检查您的代码
import random
for x in range(1,100): # this will go 1, 2, ... , 99
q = random.randint(0, 99) # q is a random number 0-99
w = random.randint(0, 99) # w is a random number 0-99
e = q * w # e is q times w
q = [] # q is now an empty list
print '%s * %s = %s' % (q,w,e)
# q is an empty list, so this is wrong
Obviously you're not trying to assign q
to an empty list each time.显然,您不是每次都试图将
q
分配给一个空列表。 You need to make a list BEFORE the loop, then keep adding to it.您需要在循环之前制作一个列表,然后继续添加。
some_list = [] # some_list is an empty list
for _ in range(99): # this will go 0, 1, ... , 98
q = random.randint(0, 99) # q is a random number 0-99
w = random.randint(0, 99) # w is a random number 0-99
e = q * w # e is q times w
some_list.append(e) # add e to the list!
print "%s * %s = %s" % (q,w,e)
# print "q * w = e"
Note that when you get a bit more advanced, you can wrap this up nicely as a list comprehension so you don't have to initialize it empty then add to it.请注意,当您变得更高级时,您可以将其很好地包装为列表推导式,这样您就不必将其初始化为空然后添加到其中。
some_list = [random.randint(0,99) * random.randint(0,99) for _ in range(99)]
# equivalent to your code
But this is still a bit past your comfort level.但这仍然有点超出您的舒适度。 One step at a time!
一步一步来! :)
:)
Things to note here that I changed:这里要注意的事情我改变了:
range
is a half-open range, eg range(1,100)
is 99 numbers, 1-99
. range
是半开范围,例如range(1,100)
是 99 个数字, 1-99
。 It does not include 100.for _ in range(some_number)
.for _ in range(some_number)
。 You use an underscore ( _
) to represent to other coders that you don't use the variable here (note that _
does not represent anything special to Python, it's just convention)_
) 向其他编码人员表示您在此处不使用该变量(请注意, _
不代表 Python 的任何特殊内容,这只是约定)for _ in range(N)
is a quick way to do something N
times. for _ in range(N)
是一种快速执行N
次操作的方法。list.append
adds to a list =) list.append
添加到列表中 =)A few notes on style:关于样式的一些注意事项:
for
loop, q=[]
in this case, if you want it to store values acrovss the several iterations of the for loop.for
循环中定义列表,在这种情况下for
q=[]
,如果您希望它在 for 循环的多次迭代中存储值。q
in this case being used both for storing a random number and as a list.q
用于存储随机数和列表。 Ways to adding to list:添加到列表的方法:
append()
function of the list
object to append a value to the list
append()
的函数list
对象的值追加到list
+
operator to concatenate a new value to the original list+
运算符将新值连接到原始列表So, here's how i would change your code:所以,这是我将如何更改您的代码:
First define the list outside the loop:首先定义循环外的列表:
import random e = []
Inside the for loop, use append()
function to add value to the master list, e
:在 for 循环内,使用
append()
函数将值添加到主列表e
:
for x in range(0,100): q=random.randint(0,99) w=random.randint(0,99) e_val=q*w # new local variable that resets at each iteration of the loop e.append(e_val) print '%s * %s = %s'%(q,w,e_val)
Alternatively, you could have replaced the line e.append(e_val)
with the following:或者,您可以将
e.append(e_val)
行替换为以下内容:
e += [e_val]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.