【问题标题】:How to check input for content in multiple lists如何检查多个列表中内容的输入
【发布时间】:2020-09-17 09:55:26
【问题描述】:

我可能有点菜鸟问题,但我如何检查“答案”的输入是否在多个列表中?我希望能够确定输入是否是“Vokalar”和“Konsonantar”的一部分。对于未来,我还希望能够确定输入是每个列表的一部分的次数。

Vokalar= ("a","e","i","o","u","y","æ","ø","å")
Konsonantar=("b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","z")

Loop = 1

while Loop == 1:
    Answer = input("Word-check ;)")
    
    if Answer in Vokalar and Konsonantar:
        print("word")
    else:
        print("not word")


【问题讨论】:

  • if (Answer in Vokalar) 和 (Answer in Konsonantar):, 不需要添加括号,只是为了便于阅读,如果您想检查它是否在至少 1 个列表中,您可以使用 '或'而不是'和'。
  • 您期待Answer = input("Word-check ;)") 的消息吗?

标签: python python-3.x list


【解决方案1】:
if Answer in Vokalar and Answer in Konsonantar:

但是,如果您稍后会有更多列表,您可以定义一个包含所有检查列表的变量并以这种方式检查:

check_lists = [Vokalar, Konsonantar, <other lists>]
if all(Answer in list_ for list_ in check_lists):

【讨论】:

    【解决方案2】:

    您必须检查答案字符串中的每个字符并将其与列表进行比较,而不是所有字符串

    Vokalar= ("a","e","i","o","u","y","æ","ø","å")
    Konsonantar= ("b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","z")
    
    Loop = 1
    
    while Loop == 1:
    
    Answer = input("Word-check : ")
    isWord = True
    for i in  Answer:
        if i in Vokalar or i in Konsonantar:
            pass
        else:
            isWord = False
    if isWord :
        print("word")
    else :
        print("not word")
    

    【讨论】:

      【解决方案3】:
      # function to count number of occurences
      def countOccurences(itemToCheck, itemsList):
          count = 0
          for item in itemsList:
              if item == itemToCheck:
                  count += 1
          return count
      
      
      Vokalar = ("a", "e", "i", "o", "u", "y", "æ", "ø", "å")
      Konsonantar = ("b", "c", "d", "f", "g", "h", "j", "k", "l", "m",
                     "n", "p", "q", "r", "s", "t", "v", "w", "x", "z")
      
      Loop = 1
      
      while Loop:
          Answer = input("Word-check ;)")
      
          if Answer in Vokalar and Answer in Konsonantar:
              vokalarOccurences = countOccurences(Answer, Vokalar)
              konsonantarOccurences = countOccurences(Answer, Konsonantar)
              print(f"{Answer} is found in both lists. Vokalar: {vokalarOccurences} times and Konsonantar: {konsonantarOccurences} times")
      
          elif Answer in Vokalar:
              vokalarOccurences = countOccurences(Answer, Vokalar)
              print(f"{Answer} is found in Vokalar list. Vokalar: {vokalarOccurences} times")
      
          elif Answer in Konsonantar:
              konsonantarOccurences = countOccurences(Answer, Konsonantar)
              print(f"{Answer} is found in Konsonantar list. Konsonantar: {konsonantarOccurences} times")
      
          else:
              print(f"{Answer} not found in any list.")
      
      

      【讨论】:

        【解决方案4】:

        要检查答案是否是单词,您可以使用字符串isalpha 的内置方法:https://docs.python.org/3/library/stdtypes.html#str.isalpha

        如果您想手动操作,最好使用sets

        def check_answer(answer: str) -> bool:
            cur_set = set(answer)
            vok = set(Vokalar) & cur_set
            kon = set(Konsonantar) & cur_set
            return len(cur_set) == len(vok) + len(kon) # len must be equal to sum of vok and kon to be a correct word
        

        为了计算出现次数,有一个内置集合(一个特殊的字典): https://docs.python.org/3/library/collections.html#collections.Counter

        from collections import Counter
        
        c = Counter(answer)
        print(c.keys())
        print(c.get('a', 0))  # to get the count for a char
        

        然后,您可以检查每个计数在哪个列表中并打印计数器:

        for elm, count in c.items():
            if elm in Vokalar:
                print(elm, "in Vokalar", count, "times")
            elif elm in Konsonantar:
                print(elm, "in Konsonantar", count, "times")
        

        请注意,您不使用这些列表管理大小写,但 isalpha 方法可以做到...

        【讨论】:

          【解决方案5】:

          要计算两个列表中答案的出现次数,您可以使用代码:

          cnt_vokalar = Vokalar.count(Answer)
          cnt_konsonantar = Konsonantar.count(Answer)
          

          然后你可以检查出现次数是否大于0:

          while Loop == 1:
              Answer = input("Word-check ;)")
              cnt_vokalar = Vokalar.count(Answer)
              cnt_konsonantar = Konsonantar.count(Answer)
              if cnt_vokalar > 0 and cnt_konsonantar > 0:
                  print("word")
              else:
                  print("not word")
          

          【讨论】:

            猜你喜欢
            • 2021-02-24
            • 1970-01-01
            • 1970-01-01
            • 2014-05-07
            • 2016-02-07
            • 2021-12-17
            • 2013-09-12
            • 2014-09-24
            • 1970-01-01
            相关资源
            最近更新 更多