2016-04-27 2 views
-1

更新:ここに私のフルコードです。私はそれが今では十分であることを望む。私は何時間もこれを解決する方法を考えてきました。いくつかのオプションは:行列、numpy、各質問の位置を保存します。私の最も深刻な恐怖は、質問に従って回答を並べ替えることができるように、コードを書き直すことです。あなたが内部の複数の文字列のリストのリストを持っていると仮定すると、あなたのデータの各行を取る最初の行のリンクされた質問に従って、回答を並べ替えるにはどうすればよいですか?

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) 

:これについて何

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) 
+1

具体的な問題を明確にしたり、必要なものを強調したりしてください。現在書かれているとおり、あなたが求めていることを正確に伝えるのは難しいです。この質問を明らかにするには、[How to Ask](http://stackoverflow.com/help/how-to-ask)ページを参照してください。 – JRodDynamite

+1

データのサブセットである – mgilbert

+0

を使用して、最小限の再現可能な例を掲載することをお勧めします。コードは長いです。私はあなたがそれをすべて理解する時間を使用することを望んでいない。質問を理解しやすくする方法を見つけるでしょう –

答えて

0

。あなたは最初の要素を取り出し、残りをランダム化します。その後、新しいリストに再度マージします。

編集:あなたは、サイクルごとのリストを持っていた場合には

、簡単です。各サイクルで次のようなことを行う必要があります。

from random import shuffle 

line = [] #your line 
lineShuffled = [] 

q = line[0] 
lineShuffled = shuffle(line[1:]) 
lineShuffled.insert(0,q) 
+0

私は毎回のリストを持っています。あなたはリストのリストのすべてのサイクルでリストをマージすることを提案していますか? –

+0

@GabrieleGiordanoは、 –

+0

の編集リストで質問のリストや回答のリストを確認しましたか? –

関連する問題