Python中利用遍历法在列表中查找特定元素
#Searching element from list by traversal from random import * Flag=1 times_Appear=0 while Flag: list_Length=int(input("Enter the length of list(Length>=25) :")) if list_Length<25: print("Length doesn"t meet the requests") Flag=0 else: List_Before=[1]*list_Length """ Initialization list """ for index_First in range(0,list_Length): List_Before[index_First-1]=randint(1,100) print("A list of length {} generated from random numbers is :".format(list_Length)) for index_First in range(0,list_Length): print(List_Before[index_First],end=" ") number_Search=int(input("Enter the number you want to search :")) for index_First in range(0,list_Length): if number_Search==List_Before[index_First]: times_Appear+=1 print("{} in list in location {}".format(number_Search,index_First)) if times_Appear==0: print("{} not in the list".format(number_Search)) print("{} appear {} times,the possiblity of appearance is {}".format(number_Search,times_Appear,times_Appear/list_Length)) Flag=int(input(" Enter 1 to cotinue and 0 to exit :")) if Flag!=1 and Flag!=0: Flag=0 print("The Flag you input is not meet the requests,force exit")