更新:这是我的完整代码。我希望现在已经足够清楚了。我一直在思考如何解决这个问题几个小时。一些选项是:矩阵,numpy,保存每个问题的位置。我最害怕的是重写代码只是为了根据问题对答案进行排序。
import random as rnd
import csv
list_one=["Question one",
"Question two"]
list_two=["Question three",
"Question four",
"Question five"]
list_three=["Question six",
"Question seven"]
list_four=["Question eight",
"Question nine"]
list_five=["Question ten"]
inventory=[listname for listname in dir() if listname.startswith("list_") ]
inventory = [globals()[listname] for listname in inventory]
questions=[]
for lst in inventory:
questions+=lst
last_label="user_list"
questions.append(last_label)
iteration=0
answers_file=csv.writer(open('Answers.csv', "wb"),delimiter=',')
all_answers=[]
dim_inv=len(inventory)
while iteration<1000:
user_list=""
random_list=rnd.sample(range(dim_inv),dim_inv)
rnd.shuffle(random_list)
inventory_position=random_list.pop()
question_list=inventory[inventory_position]
dim_question=len(question_list)
random_question=rnd.sample(range(dim_question),dim_question)
rnd.shuffle(random_question)
question_position=random_question.pop()
list_found=False
dunno=0
no=0
answers=[]
while list_found==False:
print question_list[question_position]+" Y/N/DUNNO"
answer=str(raw_input())
#Answer section
if answer.upper()=="Y":
answers.append(answer)
pick_list=[listname for listname in dir() if listname.startswith("list_") ]
print "Your list: "+pick_list[inventory_position].replace("list_","")
list_found=True
elif answer.upper()=="N":
answers.append(answer)
no+=1
elif answer.upper()=="DUNNO":
answers.append(answer)
dunno+=1
else: #12:46 now it changes the question
while True:
try:
answer=str(raw_input("Remeber to answer only with Y/N/DUNNO"+"\n"))
except ValueError:
continue
if answer.upper()=="Y" or answer.upper()=="N" or answer.upper()=="DUNNO":
if answer.upper()=="Y":
answers.append(answer)
pick_list=[listname for listname in dir() if listname.startswith("list_") ]
print "Your list: "+pick_list[inventory_position].replace("list_","")
list_found=True
elif answer.upper()=="N":
answers.append(answer)
no+=1
elif answer.upper()=="DUNNO":
answers.append(answer)
dunno+=1
break
if no>0 and len(random_list)>0:
try:
inventory_position=random_list.pop()
except IndexError:
if len(random_list)==0:
print "No more lists from which we can ask questions"
list_found=True
question_list=inventory[inventory_position]
dim_question=len(question_list)
random_question=rnd.sample(range(dim_question),dim_question)
rnd.shuffle(random_question)
try:
question_position=random_question.pop()
except IndexError:
print "No more questions for the list"
list_found=True
dunno=0
no=0
elif len(random_list)==0 and len(random_question)==0:
print "No more lists from which we can ask questions",
list_found=True
elif dunno>0 and dunno<dim_question:
try:
question_position=random_question.pop()
dim_question=len(question_list)#experimental
except IndexError:
print "No more questions for the list"
list_found=True
elif dunno==dim_question:
try:
inventory_position=random_list.pop()
except IndexError:
print "No more lists from which we can ask questions"
list_found=True
question_list=inventory[inventory_position]
dim_question=len(question_list)
random_question=rnd.sample(range(dim_question),dim_question)
rnd.shuffle(random_question)
try:
question_position=random_question.pop()
except IndexError:
print "No more questions for the list"
list_found=True
dunno=0
no=0
answers= answers + ['']*(len(questions)-len(answers))
answers.append(user_list)
answers_file.writerows([answers])
iteration+=1
all_answers.append(answers)
答案 0 :(得分:0)
怎么样:
from random import shuffle
lines = [] #your list
linesShuffled = []
for i in lines:
q = i[0]
a = shuffle(i[1:])
a.insert(0,q)
linesShuffled.append(a)
如果您的列表中包含多个字符串列表,则可以获取数据的每一行。你取出第一个元素并随机化其余元素。然后在新列表中再次合并。
修改强>
如果每个周期都有一个列表,则更容易。你应该在每个周期中做这样的事情:
from random import shuffle
line = [] #your line
lineShuffled = []
q = line[0]
lineShuffled = shuffle(line[1:])
lineShuffled.insert(0,q)