如何在列表中随机打印变量

时间:2015-11-28 14:13:14

标签: python

例如:

import random
Bandit="B"
List=["","","",""]

这样的事情:

import random
List=["","","",""]
List[i]='B'
for i in range(0,2):
    print(random.randint(List[i]))

Aka不起作用

我希望它能用于以下功能打印出来:

print["B","","B",""] or ["","B","","B"] and all the other combinations

2 个答案:

答案 0 :(得分:1)

抱歉,我不知道你是否希望程序随机放置强盗或让它随机选择两种组合?去了两个中的第一个,但如果你想要后者只是说。希望这会有所帮助:)

import random
numb = random.randint(0,3)
bandit = "B"
list = ["","","",""]
list[numb] = bandit
print(list)

答案 1 :(得分:0)

我不太清楚为什么要使用随机功能,但既然你知道你想要'B'出现在哪里,你就可以。

List[i] = 'B'    # i is an integer, and it is where you want your thing to be

List[0] = 'B'    # 0 is where a list starts 
List[2] = 'B'    

结果

['B','','B','']