首页 > 解决方案 > Python 需要帮助以获取项目 DUE TMRW 的列表,哈哈

问题描述

我的程序目前看起来像这样:

import random

answer = ""
correct_math = ["2", "3", "1"]
math_problems = ["What is the square root of 25?   1.) 4   2.) 5   3.) 6: ", "What is 97 + 113?   1.) 200   2.) 100   3.) 210: ", "What is 954 - 164?   1.) 790   2.) 835   3.) 756: "]

answer = input("STUDY GUIDE: What subject are you trying to study? Math (1) Science (2) History (3) : ")

if answer == "1":
    answer = input(random.choice(math_problems))

我正在尝试使它调用一个随机问题来研究,但我不知道如何使它随机调用的问题等于答案列表中的答案。我将它们设置为 1:1,因此问题列表中的第一项在correctmath列表中按顺序回答。如果有意义的话,我只需要知道如何调用与随机抽取的问题有关的正确答案。此外,我的代码并没有像那样隔开,只是不会让我添加新段落。

标签: pythonlist

解决方案


以下是如何随机排序两个 numpy 数组并保持它们同时排序:

import numpy as np

data = np.array(["a","b","c","d"])
labels = np.array([1,2,3,4])
# create a numbered list of your indexes and shuffle it
ind = list(range(len(data)))
np.random.shuffle(ind)
data_rand = data[ind]
labels_rand = labels[ind]

我还做了一个快速的 Stackoverflow 搜索,这里还有一些其他方法:Better way to shuffle two numpy arrays in unison


推荐阅读