【问题标题】:Selecting multiple elements from array python从数组python中选择多个元素
【发布时间】:2016-10-07 23:21:50
【问题描述】:

这是一个非常基本的问题,但这里是:

我想创建一个数组,然后想将用户输入与数组中的那些元素进行比较。

如果匹配一个元素,则函数 1 将运行。如果数组中的两个元素匹配,则将运行替代函数,依此类推。

目前我只能使用没有指向数组的链接的 IF 语句,如下所示:

def stroganoff():
    print ("You have chosen beef stroganoff")
    return

def beef_and_ale_pie():
    print ("You have chosen a beef and ale pie")
    return

def beef_burger():
    print ("You have chosen a beef burger")
    return

ingredients = ['beef','mushrooms','ale','onions','steak','burger']

beef = input("Please enter your preferred ingredients ")

if "beef" in beef and "mushrooms" in beef:
    stroganoff()
elif "beef" in beef and "ale" in beef:
    beef_and_ale_pie()
elif "beef" in beef and "burger" in beef:
    beef_burger()

如前所述,这对你们中的一些人来说是基本的东西,但感谢您的关注!

【问题讨论】:

  • array 是指array 还是list?就像在 python 中一样,两者都是相似但不同的东西。如果你想列出,你已经有一个ingredients = ['beef','mushrooms','ale','onions','steak','burger']。请你在这里稍微清楚一下你的意图。
  • 当我说数组时——我实际上是指列表!但是我无法访问该列表来按照我的描述进行操作。它按原样工作,但我希望能够添加到列表中并进行有效的编码以将输入与列表进行比较,当两个单词匹配时,然后运行特定的函数,而不必像当前那样拥有长的 ELIF 语句。谢谢!

标签: python arrays python-3.x


【解决方案1】:

因为你只能使用 IF 语句

beef=input().split()
#this splits all the characters when they're space separated
#and makes a list of them

您可以使用您的"beef" in beef and "mushrooms" in beef,它应该会按照您的预期运行

【讨论】:

    【解决方案2】:

    所以我理解你的问题,所以你想知道,用户输入了多少ingredients

    ingredients = {'beef','mushrooms','ale','onions','steak','burger'}
    
    # assume for now the inputs are whitespace-separated:
    choices = input("Please enter your preferred ingredients ").split()
    
    num_matches = len(ingredients.intersection(choices))
    print('You chose', num_matches, 'of our special ingredients.')
    

    【讨论】:

      【解决方案3】:

      你可以这样做:

      # Dictionary to map function to execute with count of matching words
      check_func = {
          0: func_1,
          1: func_2,
          2: func_3,    
      } 
      
      ingredients = ['beef','mushrooms','ale','onions','steak','burger']
      user_input = input()
      
      # Convert user input string to list of words
      user_input_list = user_input.split()
      
      # Check for the count of matching keywords
      count = 0
      for item in user_input_list:
          if item in ingredients:
              count += 1
      
      # call the function from above dict based on the count
      check_func[count]()
      

      【讨论】:

      • 谢谢,但是当我运行它时,我收到以下错误消息: Traceback(最近一次调用最后一次):文件“C:/Users/Dan/Documents/Python/lists2.py”,第 3 行, 在 0: func_1, NameError: name 'func_1' is not defined
      • 错误信息是什么?而且我相信您已经在代码中定义了func_1 func_2 函数,或者将在dict 中用您自己的函数替换这些函数:)
      • 跟我想的完全一样。我只是给了你示例代码。您必须用您的stroganoffbeef_and_ale_pie 或您想要的任何其他功能替换它们。注意:函数应该没有()
      • 您好,它计算元素的数量,但是有没有办法从列表中识别实际元素 - 关键字,和/或自动运行该功能。无论输入如何,它目前只运行beef_burger。例如;用户输入beef and ale,程序说:我们已经识别出关键词beef and ale;那么程序运行beef_and_ale_pie 函数呢?非常感谢您的帮助,谢谢!
      • 我已经弄清楚如何识别找到的关键字但仍在考虑如何自动运行函数!
      猜你喜欢
      • 1970-01-01
      • 2017-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多