【问题标题】:finding a user defined item in a list using a for loop使用 for 循环在列表中查找用户定义的项目
【发布时间】:2017-06-23 16:05:46
【问题描述】:

我有以下代码:

def main():
    list=["chocolate", "jelly", "biscuits"]
    eat=input("What do you wanna eat?")
    for i in list:
        if list.index(eat)==eat:        
            break
        else:
            break

    print("We have that in tray:", list.index(eat))
main()

在 VB.Net 中,这将在 For 循环中很容易地工作,这将是执行此操作的惯用方式。为什么不在这里?此外,这不是 stackoverflow 上另一个问题的重复,其中用户提供了做类似事情的替代方法/pythonic 建议。

出于教学目的,我需要使用 for 循环,并更正上面给出的结构。

如果用户输入的内容不是列表中的内容,我如何添加将打印“该项目不在列表中”的条件逻辑。我正在寻找最简单的原始代码修复方法。

我确实尝试了以下操作,但出现了逻辑错误。有了解决方案。

尝试#1:

def main():
    list=["chocolate", "jelly", "biscuits"]
    eat=input("What do you wanna eat?")
    for i in list:
        if list.index(eat)==eat:        
            break
        else:
            print("that is not in the list")

    print("We have that in tray:", list.index(eat))
main()

错误:

>>> 
What do you wanna eat?jelly
that is not in the list
that is not in the list
that is not in the list
We have that in tray: 1
>>> 

试试 2

def main():
    list=["chocolate", "jelly", "biscuits"]
    eat=input("What do you wanna eat?")
    for i in list:
        if list.index(eat)==eat:        
            break
        else:
            break
    print("that is not in the list")

    print("We have that in tray:", list.index(eat))
main()

错误:

>>> 
What do you wanna eat?jelly
that is not in the list
We have that in tray: 1
>>> 

尝试#3:

def main():
    list=["chocolate", "jelly", "biscuits"]
    eat=input("What do you wanna eat?")
    for i in list:
        if list.index(eat)==eat:        
            break
        elif list.index(eat)!=eat:
            print("that is not in the list")

    print("We have that in tray:", list.index(eat))
main()

错误:

>>> 
What do you wanna eat?jelly
that is not in the list
that is not in the list
that is not in the list
We have that in tray: 1
>>> 

【问题讨论】:

  • 你说的最简单的修复是什么意思?您如何衡量简单性?
  • 我的意思是,我不希望人们建议使用字典、lambda 或者我需要摆脱 for 循环!。我只希望我放在那里的代码得到修复和评论。因此,在 for 循环中添加了一条打印行,表示“不在列表中”。我希望它使用 FOR 循环修复 - 不是没有。这是关键
  • ...或枚举!
  • 知道了,答案已更新
  • list.index(x) 返回列表中第一个值为 x 的项目的索引。如果没有这样的项目是错误的。您试图将此 index 与您给出的值进行比较,吃。

标签: python list python-3.x loops for-loop


【解决方案1】:

这里是:

def main():
    list=["chocolate", "jelly", "biscuits"]
    eat=input("What do you wanna eat?")
    for element in list:
        index = list.index(element)
        if element == eat:
          print("We have that in tray:", index)
          break
          # If the element is found, then print the message and stop the loop

        elif index == len(list) - 1:
          # Index starts form 0, then if you have a list with n elements
          # and you reach the element with index n-1, you have reached
          # the last element.
          print("that is not in the list")
          # if the element is the last in the list, and it's not
          # equal to the user input, then the user input is not in the
          # list. Print the error message.

main()

并使用enumerate

def main():
    list=["chocolate", "jelly", "biscuits"]
    eat=input("What do you wanna eat?")
    for index,element in enumerate(list):
        if element == eat:
          print("We have that in tray:", index)
          break
        elif index == len(list) - 1:
          print("that is not in the list")

main()

【讨论】:

  • 我想要一个没有其他 python 特性(例如枚举)的修复......但是正如你提到的枚举,你能否提供(对其他人也有帮助)最简单的方法使用枚举。 (忽略我的原始代码,并使用枚举寻求您的解决方案)。但是,我仍然在等着看是否有人可以回答这个问题。在 python 中使用 for 循环以及为什么它似乎违反常识,或者其他一些语言无论如何都有的那种感觉......!
  • 另外,您能否解释一下这一行中的“枚举”一词:for index,element in enumerate(list): 并解释这一行 elif index == len(list) - 1:(尤其是-1,以及它在做什么)。将 cmets 添加到您的解决方案中就可以了。提前谢谢你
  • enumerate(参考之前版本的答案)获取列表并生成像(index, element)这样的元组列表。例如,您的列表["chocolate", "jelly", "biscuits"] 将变为[(0,"chocolate"), (1,"jelly"), (2,"biscuits")]。然后,通过循环该元组列表,对于enumerate(list) 中的每个元组,您将获得index 中元组的第一个元素和element 中的第二个元素。
  • 另外,enumerate(list) 并没有真正创建一个元组列表,而是一个 iterator,它是 python 中一个特殊的可迭代对象。进一步阅读:anandology.com/python-practice-book/iterators.html
  • 谢谢伊拉里奥。是否有可能也使用枚举发布对同一问题的最优雅解决方案的答案。这对任何学习者都有帮助...
【解决方案2】:
def main():
    list=["chocolate", "jelly", "biscuits"]
    eat=input("What do you wanna eat?")
if eat in list:
    print("We have that in tray:", list.index(eat))
else:
    print("That is not in the list")

main()

【讨论】:

  • 这个,我可以做的很简单,但是如果你阅读了我上面的问题,我正在尝试,为了教学目的,更好地理解 python 中 for 循环的使用。在 VB 中,上述方法会起作用。我想在 FOR 循环中添加“else”。或者那不可能?您的解决方案虽然很好,但只是去掉了我不想要的 for 循环。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-04
  • 2019-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-21
  • 1970-01-01
相关资源
最近更新 更多