【问题标题】:Python, random words into questions [duplicate]Python,随机词成问题[重复]
【发布时间】:2015-01-08 04:39:18
【问题描述】:

我有两个列表:

wordlists1 = ["hot","summer", "hard", "dry", "heavy", "light", "weak", "male",
             "sad", "win",  "small","ignore", "buy", "succeed", "reject", "prevent", "exclude"]

wordlists2 = ["cold", "winter", "soft", "wet", "light", "darkness", "strong", "female", "happy", "lose", "big",
         "pay attention", "sell", "fail", "accept", "allow", "include"]

我使用 random.choice 从每个列表中选择一个单词。一旦我有了单词,我需要将它们作为问题打印出来。例如,如果选择 hot 和 weak 它应该打印:“Hot is to cold as weak is to___?”

我真的需要这方面的帮助,我们将不胜感激。

我的代码:

import random 

wordlists1 =["hot","summer", "hard", "dry", "heavy", "light", "weak", "male", "sad", "win", "small","ignore", "buy", "succeed", "reject", "prevent", "exclude"] 
wordlists2 =["cold", "winter", "soft", "wet", "light", "darkness", "strong", "female", "happy", "lose", "big", "pay attention", "sell", "fail", "accept", "allow", "include"] 
randomword1=random.choice(wordlists1) 
randomword2=random.choice(wordlists2)

【问题讨论】:

  • 向我们展示您目前拥有的代码,即使它已损坏。

标签: python


【解决方案1】:

我可能会做类似的事情

>>> wpairs = list(zip(wordlists1, wordlists2))
>>> example, question = random.sample(wpairs, 2)
>>> "{} is to {} as {} is to ?".format(example[0], example[1], question[0])
'small is to big as summer is to ?'

首先,我将这两个列表组合成一个对列表:

>>> wpairs = list(zip(wordlists1, wordlists2))
>>> wpairs
[('hot', 'cold'), ('summer', 'winter'), ('hard', 'soft'), ('dry', 'wet'), ('heavy', 'light'), ('light', 'darkness'), ('weak', 'strong'), ('male', 'female'), ('sad', 'happy'), ('win', 'lose'), ('small', 'big'), ('ignore', 'pay attention'), ('buy', 'sell'), ('succeed', 'fail'), ('reject', 'accept'), ('prevent', 'allow'), ('exclude', 'include')]

然后我会使用random.sample 选择其中两个:

>>> example, question = random.sample(wpairs, 2)
>>> example, question
(('weak', 'strong'), ('heavy', 'light'))

在这里使用random.sample 的一个主要优点是您不必担心两次绘制同一对(没有“弱是强,弱是强?”的问题。)

在此之后,我们可以制作一个问题字符串:

>>> "{} is to {} as {} is to ?".format(example[0], example[1], question[0])
'weak is to strong as heavy is to ?'

【讨论】:

  • 这显然是最好的答案。以这种方式使用 sample 避免了对 SAME 元素进行两次采样的情况,这在 sample/choice 被调用两次的答案中是可能的。
【解决方案2】:
wordlists1 = ["hot","summer", "hard", "dry", "heavy", "light", "weak", "male",
                     "sad", "win",  "small","ignore", "buy", "succeed", "reject", "prevent", "exclude"]

wordlists2 = ["cold", "winter", "soft", "wet", "light", "darkness", "strong", "female", "happy", "lose", "big",
                 "pay attention", "sell", "fail", "accept", "allow", "include"]

import random

t = zip(wordlists1, wordlists2)
t1, t2 = random.sample(t, 2)
print '%s is to %s as %s is to ___? (%s)' % (t1[0], t1[1], t2[0], t2[1])

应该打印类似的东西 dry is to wet as ignore is to ___? (pay attention)

更新:我从random.choice切换到random.sample(t, 2)。这是执行此操作的更好方法。 (根据 DSM 的建议,但我也想更新我的代码)。

【讨论】:

    【解决方案3】:
    import random 
    wordlists1 =["hot","summer", "hard", "dry", "heavy", "light", "weak", "male", "sad",      "win", "small","ignore", "buy", "succeed", "reject", "prevent", "exclude"] 
    wordlists2 =["cold", "winter", "soft", "wet", "light", "darkness", "strong", "female",     "happy", "lose", "big", "pay attention", "sell", "fail", "accept", "allow", "include"]
    l1=len(wordlists1)
    index1=int(random.random()*l1)
    index2=int(random.random()*l1)
    myquestion=wordlists1[index1]+" is to "+wordlists2[index1]+" as "+ wordlists1[index2]+"    is to___?"
    print myquestion
    

    【讨论】:

      【解决方案4】:

      您使用了两次random.choice,这使得randomword1 与randomword2 在列表中的位置不同。改用random.randint,每次获取统一索引:

      import random 
      wordlists1 =["hot","summer", "hard", "dry", "heavy", "light", "weak", "male", "sad", "win", "small","ignore", "buy", "succeed", "reject", "prevent", "exclude"] 
      wordlists2 =["cold", "winter", "soft", "wet", "light", "darkness", "strong", "female", "happy", "lose", "big", "pay attention", "sell", "fail", "accept", "allow", "include"] 
      idx1 = random.randint(0, len(wordlists1)-1)
      idx2 = random.randint(0, len(wordlists1)-1)
      words_to_choose = (wordlists1[idx1], wordlists2[idx1], wordlists1[idx2], wordlists2[idx2])
      print '%s is to %s as %s is to ___? (answer: %s)'%words_to_choose
      
      #OUTPUT: reject is to accept as exclude is to ___? (answer: include)
      

      【讨论】:

        【解决方案5】:

        使用随机选择两个单词索引并使用这些索引生成问题并检查答案是否正确,如下所示:

        import random
        def makeQuestion():
            indexes = range(len(wordlists1))
            word1 = random.choice(indexes)
            word2 = random.choice(indexes)
            ans = raw_input("{} is to {} as {} is to___? ".format(wordlists1[word1], wordlists2[word1], wordlists1[word2]))
            if ans.strip().lower() == wordlists2[word2]:
               print True
            else:
               print False
        

        演示:

        >>> wordlists1 = ["hot","summer", "hard", "dry", "heavy", "light", "weak", "male",
        ...              "sad", "win",  "small","ignore", "buy", "succeed", "reject", "prevent", "exclude"]
        >>> wordlists2 = ["cold", "winter", "soft", "wet", "light", "darkness", "strong", "female", "happy", "lose", "big",
        ...          "pay attention", "sell", "fail", "accept", "allow", "include"]
        >>> import random
        >>> def makeQuestion():
        ...     indexes = range(len(wordlists1))
        ...     word1 = random.choice(indexes)
        ...     word2 = random.choice(indexes)
        ...     ans = raw_input("{} is to {} as {} is to___? ".format(wordlists1[word1], wordlists2[word1], wordlists1[word2]))
        ...     if ans.strip().lower() == wordlists2[word2]:
        ...        print True
        ...     else:
        ...        print False
        ... 
        >>> makeQuestion()
        succeed is to fail as sad is to___? happy
        True
        >>> makeQuestion()
        prevent is to allow as ignore is to___? pay attention
        True
        >>> makeQuestion()
        exclude is to include as heavy is to___? cold
        False
        

        【讨论】:

          【解决方案6】:
          from random import randint
          
          wordlists1 = ["hot","summer", "hard", "dry", "heavy", "light", "weak", "male",
                       "sad", "win",  "small","ignore", "buy", "succeed", "reject", "prevent", "exclude"]
          
          wordlists2 = ["cold", "winter", "soft", "wet", "light", "darkness", "strong", "female", "happy", "lose", "big",
                   "pay attention", "sell", "fail", "accept", "allow", "include"]
          
          index1 = randint(0, len(wordlists1) - 1)
          
          index2 = randint(0, len(wordlists2) - 1)
          
          answer = wordlists2[index2]
          
          print ("Q : %s is %s as %s is to ________ ? " % (wordlists1[index1], wordlists2[index1], wordlists1[index2]))
          
          user_input = raw_input("A : ")
          
          if user_input.lower() != answer:
          
                  print ("Answer is %s" % answer)
          else:
          
                  print ("Correct Answer")
          

          【讨论】:

            【解决方案7】:

            生成一个从 0 到列表长度的随机数。这个数字代表从您的列表中选择一个随机索引。一旦你“随机”选择了你的词,就在你的问题中使用它们

            【讨论】:

              【解决方案8】:
              from __future__ import print_function
              import random
              
              __author__ = 'lve'
              
              wordlists1 = ["hot", "summer", "hard", "dry", "heavy", "light", "weak", "male",
                            "sad", "win", "small", "ignore", "buy", "succeed", "reject", "prevent", "exclude"]
              
              wordlists2 = ["cold", "winter", "soft", "wet", "light", "darkness", "strong", "female", "happy", "lose", "big",
                            "pay attention", "sell", "fail", "accept", "allow", "include"]
              
              answer_string = ''
              
              random_question_index = random.randrange(len(wordlists1))
              
              answer_string += '{} is to {} as '.format(wordlists1.pop(random_question_index).capitalize(), wordlists2.pop(random_question_index))
              
              random_answer_index = random.randrange(len(wordlists1))
              answer_string += '{} is to___? \nAnswer is {}'.format(wordlists1.pop(random_question_index),
                                                                    wordlists2.pop(random_question_index).upper())
              print(answer_string)
              

              【讨论】:

                猜你喜欢
                • 2014-06-14
                • 1970-01-01
                • 2013-09-20
                • 2014-08-23
                • 1970-01-01
                • 2013-11-30
                • 2016-09-26
                相关资源
                最近更新 更多