List Python-如何向列表中添加随机数?
大家好 有没有人有办法在列表中添加一个随机数?我试图得到一个列表来记录循环中生成的随机数。下面是循环中的代码示例:List Python-如何向列表中添加随机数?,list,python-3.x,random,List,Python 3.x,Random,大家好 有没有人有办法在列表中添加一个随机数?我试图得到一个列表来记录循环中生成的随机数。下面是循环中的代码示例: stuff = {'name': 'Jack', 'age': 30, 'height': '6 foot 9 inches'} tester = [0] print(tester) tester.append[random.randint(1, len(stuff))] print(tester) 显然,random.randint的输出是不可下标的,但我不知道如何编写
stuff = {'name': 'Jack', 'age': 30, 'height': '6 foot 9 inches'}
tester = [0]
print(tester)
tester.append[random.randint(1, len(stuff))]
print(tester)
显然,random.randint的输出是不可下标的,但我不知道如何编写它
提前感谢您的帮助
tester.append[random.randint(1, len(stuff))]
# wrong ^ wrong ^
# should be
tester.append(random.randint(1, len(stuff)))
方法(如append
)用括号而不是括号调用
方法(如append
)用括号而不是括号调用
方法(如append
)用括号而不是括号调用
方法(如append
)是用括号而不是括号调用的。很简单,试试这个
from random import randint # import randint from random
listone = [] # Creating a list called listone
for i in xrange(1,10): # creating a loop so numbers can add one by one upto 10 times
ic = randint(1,10) # generating random numbers from 1 to 10
listone.append(ic) # append that numbers to listone
pass
print(listone) # printing list
# for fun you can sort this out ;)
print(sorted(listone))
很简单,试试这个
from random import randint # import randint from random
listone = [] # Creating a list called listone
for i in xrange(1,10): # creating a loop so numbers can add one by one upto 10 times
ic = randint(1,10) # generating random numbers from 1 to 10
listone.append(ic) # append that numbers to listone
pass
print(listone) # printing list
# for fun you can sort this out ;)
print(sorted(listone))
很简单,试试这个
from random import randint # import randint from random
listone = [] # Creating a list called listone
for i in xrange(1,10): # creating a loop so numbers can add one by one upto 10 times
ic = randint(1,10) # generating random numbers from 1 to 10
listone.append(ic) # append that numbers to listone
pass
print(listone) # printing list
# for fun you can sort this out ;)
print(sorted(listone))
很简单,试试这个
from random import randint # import randint from random
listone = [] # Creating a list called listone
for i in xrange(1,10): # creating a loop so numbers can add one by one upto 10 times
ic = randint(1,10) # generating random numbers from 1 to 10
listone.append(ic) # append that numbers to listone
pass
print(listone) # printing list
# for fun you can sort this out ;)
print(sorted(listone))
在您的代码中执行此操作
import random
stuff = {'name': 'Jack', 'age': 30, 'height': '6 foot 9 inches'}
tester = [0]
print(tester)
tester.append(random.randint(1, len(stuff)))
print(tester)
在您的代码中执行此操作
import random
stuff = {'name': 'Jack', 'age': 30, 'height': '6 foot 9 inches'}
tester = [0]
print(tester)
tester.append(random.randint(1, len(stuff)))
print(tester)
在您的代码中执行此操作
import random
stuff = {'name': 'Jack', 'age': 30, 'height': '6 foot 9 inches'}
tester = [0]
print(tester)
tester.append(random.randint(1, len(stuff)))
print(tester)
在您的代码中执行此操作
import random
stuff = {'name': 'Jack', 'age': 30, 'height': '6 foot 9 inches'}
tester = [0]
print(tester)
tester.append(random.randint(1, len(stuff)))
print(tester)
我的猜测:你想生成4个随机数并将其添加到测试仪列表?我的猜测:你想生成4个随机数并将其添加到测试仪列表?我的猜测:你想生成4个随机数并将其添加到测试仪列表?我的猜测:你想生成4个随机数并将其添加到测试仪列表?