![](/img/trans.png)
[英]In Python how to associate words to a list and then randomly print a value from the list?
[英]How print a variable randomly in a list
例如:
import random
Bandit="B"
List=["","","",""]
像這樣:
import random
List=["","","",""]
List[i]='B'
for i in range(0,2):
print(random.randint(List[i]))
阿卡行不通
我想要它打印出來的功能如:
print["B","","B",""] or ["","B","","B"] and all the other combinations
抱歉,我無法確定是要程序隨機放置強盜還是要在兩種組合之間隨機選擇? 嘗試了兩者中的第一個,但是如果您想稍后再說。 希望這可以幫助 :)
import random
numb = random.randint(0,3)
bandit = "B"
list = ["","","",""]
list[numb] = bandit
print(list)
我不太確定為什么要使用隨機函數,但是由於您知道要在哪里出現“ 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','']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.