2017-06-10 101 views

回答

9

使用random.choicerandom.sample是獲得一堆物品:

list2.insert(1, random.choice(list1)) 
+0

它的工作,謝謝! – FloRaptor

0

在這裏,你走了,假設你想從列表1中刪除:

list2.insert(1, list1.pop(random.randrange(len(list1))) 
+3

你從哪裏得到這個假設? – Uriel

+1

原始問題的含糊不清 –

1
>>> import random 
>>> list1 = ["a", "b", "c", "d"] 
>>> list2 = ["z", "y", "x", "w"] 
>>> list2.insert(1, random.sample(list1, 1)[0]) 
>>> list2 
['z', 'b', 'y', 'x', 'w'] 

只需修改您的樣本,但索引第0個元素去除大括號。

+0

@Uriel明白了,謝謝。英語並不是我最強的西裝。 –

+1

當然。 +1爲了糾正你的帖子而煩惱。繼續。 – Uriel

相關問題