如何在列表中查找项目(不使用" in"方法)?

时间:2016-06-18 07:03:34

标签: python list python-3.x search

我试图在列表中找到一个项目而不使用"在"方法。我尝试使用循环来做到这一点。代码执行成功,但两者都给出了集体结果(找到的项目以及未找到的项目)。我尝试使用break语句修复它。它以某种方式工作。但仍未达到预期的结果。

用python3.2编写的代码是: -

list=[]
item=""
while item!='DONE':
    item=input("Enter the number, To discontinue enter 'DONE': ")

    if item.isdigit():
        item=int(item)
        list.append(item)
    else:
        list.append(item)

del list[-1]
print("The list is: ",list)

num=int(input("Enter the number you want to search in list: "))
def search(list,num):
    for i in range(len(list)):
        if list[i]==num:
            print("Item found")
            break
        else:
            print("Not found")
    return
search(list,num)

请建议我如何修改它以搜索"整数"以及" string"类型元素。我的代码适用于整数类型元素。

4 个答案:

答案 0 :(得分:1)

执行此操作时,您尝试将字符串转换为int

num = int(input("Enter the number you want to search in list: "))

当输入不是数字时,这将引发异常。

相反,为什么不使用isdigit()执行与代码的第一部分相同的操作?如果是数字,则将其转换为int,如果不是数字,则将其保留为字符串。那么你的代码也适用于非数字。

num = input("Enter the number you want to search in list: ")
if num.isdigit():
    num = int(num)

或者,第二种解决方案是不在代码的第一部分或第二部分中向int投射任何内容。换句话说,把所有东西都保留为字符串,即

while item != 'DONE':
    item = input("Enter the number, To discontinue enter 'DONE': ")
    list.append(item)

...

num = input("Enter the number you want to search in list: ")

答案 1 :(得分:0)

确实没有必要区分整数和字符串 - 它们只是,Python可以以同样的方式处理你想要的情况下即可。鉴于这一事实,您可以按照以下几行重写代码(其中包括可用于简化逻辑和以更标准的方式组织代码的其他几种技术 - 请参阅PEP 8 - Style Guide for Python Code):

def search(a_list, value):
    for item in a_list:  # note: no need to use an index to iterate a list
        if item == value:
            print("Value found")
            break
    else:  # only executes if loop finishes without break
        print("Value not found")

my_list = []  # note: avoid using the names of built-ins like "list"
while True:
    value = input("Enter an value, to discontinue enter 'DONE': ")
    if value == 'DONE':
        break
    my_list.append(value)

print("The list is: ", my_list)

value = int(input("Enter the value you want to search for in the list: "))
search(my_list, value)

答案 2 :(得分:0)

由于这是一项学习练习,我会将其作为搜索提交,以避免in。其他人会声称这是一个肮脏的腐烂作弊。为什么呢?

def search(inlist, item):
    print(item, end="")
    if inlist.__contains__(item):
        print(" found")
    else:
        print(" not found")

mylist = []
item = None
while item != 'DONE':
    item = input("Enter the number, To discontinue enter 'DONE': ")
    mylist.append(item)

while True:
    num = input("Enter the item you want to search for: ")
    search(mylist, num)

同样在学习的精神中,这是一个使用集合的解决方案。如果我们使用集合,我们可能不会在第一时间创建列表,但我们假设列表来自其他地方。

这里的要点是,如果你有一个大的列表和大量的搜索,那么使用一个集合可能比迭代列表更快。

def search(inset, item):
    print(item, end="")

    if inset & set((item,)):
        print(" found")
    else:
        print(" not found")

mylist = []
item = None
while item != 'DONE':
    item = input("Enter the number, To discontinue enter 'DONE': ")
    mylist.append(item)

myset = set(mylist)
while True:
    num = input("Enter the item you want to search for: ")
    search(myset, num)

答案 3 :(得分:0)

使用:

def seach(l, elm):
    try:
        l.index(elm)
        print "found"
    except:
        print "not found"

它比自定义循环更加pythonic。

PS:当你处于学习阶段时,不要试图找到执行任务的方法。 'in'关键字是为它制作的。