猿问

需要运行以下在循环中运行的 If 语句

我需要在给定的列表中找到一个特定的数字,该数字按升序排列。我已经写了所有的if语句。但不知道如何循环运行它们。支持找到一种方法,如何在循环中运行此内容以及列表中是否有数字可用。例如:


list_of_number=[1,2,3,4,5,6,7]

number_to_identify=6

所以需要输出,6是否在列表下。但请注意,我知道我们可以直接使用“FOR”循环找到它。我想用下面的方法来计算


方法需要遵循:找到中间元素并与 进行比较。根据决策(更大/更小/相等),进行输出并运行此输出,直到找到列表中是否有可用的数字。number_to_identify


这是我用“If语句”写的编码。但需要循环此 If 语句以查找给定列表中的数字是否可用。


list_of_number=[1,2,3,4,5,6,7]

number_to_identify=6


Start_Index=0

End_Index=len(list_of_number)

new_list=[]


middle_index=(End_Index-Start_Index)/2

middle_Element=list_of_number[int(middle_index)]

print(middle_index)

print(middle_Element)


if middle_Element<number_to_identify:

    Start_Index=middle_index

    new_list=list_of_number[int(Start_Index):int(End_Index)]

    print(new_list)

elif number_to_identify<middle_Element:

    End_Index=middle_index

    new_list=list_of_number[int(Start_Index):int(End_Index)]

    print(new_list)

else:

    if middle_Element==number_to_identify:

        print("Number is available within the list")


慕桂英4014372
浏览 136回答 3
3回答

慕雪6442864

使用 while 循环,如果找到该数字,则中断:while True:&nbsp; &nbsp; middle_index = (end_index-start_index) / 2&nbsp; &nbsp; ...&nbsp; &nbsp; elif middle_element == number_to_identify:&nbsp; &nbsp; &nbsp; &nbsp; print("Number is in the list")&nbsp; &nbsp; &nbsp; &nbsp; break顺便说一句,我重新拼写了变量以匹配Python的推荐样式。

红糖糍粑

def identify(num,lst):&nbsp; &nbsp; start = int((len(lst)/2) - ((len(lst)/2) % 1))&nbsp; &nbsp; while True:&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if num > lst[start]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start += 1&nbsp; &nbsp; &nbsp; &nbsp; if num < lst[start]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start -= 1&nbsp; &nbsp; &nbsp; &nbsp; if num == lst[start]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Found number at index: {0}".format(start))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return lst[start]list_of_number=[1,2,3,4,5,6,7]number_to_identify=6identify(number_to_identify, list_of_number)这将为您解决问题。如果列表的长度是偶数,它将从中间右边的第一个元素开始。

qq_遁去的一_1

以下代码有点笨重。但它会起作用。如果需要进行任何修改,请提出建议,如果我们能使它更容易。def find_number(list_number,num_to_check):&nbsp; &nbsp; Start_Index=0&nbsp; &nbsp; End_Index=len(list_number)-1&nbsp; &nbsp; new_list=list_number&nbsp; &nbsp; not_found=False&nbsp; &nbsp; while len(new_list)>2:&nbsp; &nbsp; &nbsp; &nbsp; middle_index=(End_Index-Start_Index)/2&nbsp; &nbsp; &nbsp; &nbsp; middle_Element=new_list[int(middle_index)]&nbsp; &nbsp; &nbsp; &nbsp; if new_list[0]==num_to_check or new_list[-1]==num_to_check:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Number in List")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; #If number is in Second Half of the list&nbsp; &nbsp; &nbsp; &nbsp; elif middle_Element<num_to_check:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Start_Index=middle_index&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new_list=new_list[int(Start_Index):int(End_Index+1)]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(new_list)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End_Index=len(new_list)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Start_Index=0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(End_Index)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; not_found=False&nbsp; &nbsp; #If number is in First Half of the list&nbsp; &nbsp; &nbsp; &nbsp; elif num_to_check<middle_Element:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End_Index=middle_index&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new_list=new_list[int(Start_Index):int(End_Index+1)]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(new_list)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; End_Index=len(new_list)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(End_Index)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; not_found=False&nbsp; &nbsp; #If Number is match with the middle number&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; not_found=True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; if not_found:&nbsp; &nbsp; &nbsp; &nbsp; print("Number in List")&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print("Number not in List")l1 = [10,22,43,56,65,79,88,92,102]n1 = 93find_number(l1,n1)
随时随地看视频慕课网APP

相关分类

Python
我要回答