如何与一个随机化列表并行迭代两个列表

时间:2015-04-24 08:43:55

标签: list python-2.7 iteration tuples

This thread解释了如何遍历两个并行列表。

然而,当我将该课程应用于洗牌列表时,我收到以下错误:" zip参数#2不支持迭代。"为什么洗牌列表不支持迭代?我还能如何随机化答案选择的顺序?

import random

def answers():
    answerList = random.shuffle([answer1, answer2, answer3, correct])
    numberList = ["1: ", "2: ", "3: ", "4: "]
    for x,y in zip(numberList,answerList):
        print x,y 

prompt = "What is the average migrating speed of a laden swallow?"
answer1 = "Gas or electric?"
answer2 = "Metric or English?"
answer3 = "Paper or plastic?"
correct = "African or European?"

print prompt
answers()

2 个答案:

答案 0 :(得分:2)

random.shuffle随机播放 。请执行以下操作:

answerList = [answer1, answer2, answer3, correct]
random.shuffle(answerList)

答案 1 :(得分:0)

感谢pacholik的解决方案。

import random

def answers():
    answerList = [answer1, answer2, answer3, correct]
    random.shuffle(answerList)
    numberList = ["1: ", "2: ", "3: ", "4: "]
    for x,y in zip(numberList,answerList):
        print x,y 

prompt = "What is the average migrating speed of a laden swallow?"
answer1 = "Gas or electric?"
answer2 = "Metric or English?"
answer3 = "Paper or plastic?"
correct = "African or European?"

print prompt
answers()