Checking number in a list to see if they are beside each other - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Checking number in a list to see if they are beside each other (/thread-20677.html) |
Checking number in a list to see if they are beside each other - Allaye - Aug-25-2019 List1 =[2,3,4,9,9,5,1] def checkList(List1): for i in range(len(List1 - 1)): if List1[i] == 9 and List1[i+1] == 9: return True return falsei want to check if the number 9 comes b4 any other number in the list but my problem is the code does not return true or false RE: Checking number in a list to see if they are beside each other - Yoriz - Aug-25-2019 List1 =[2,3,4,9,9,5,1]lowercase for variable names. for i in range(len(List1 - 1)):-1 should be taken from the result of calling len(List1) Don't use range to loop over items, loop over them directly. Indentation is wrong return falseShould be capital F False A better way function check_list that takes in list as it parameter
RE: Checking number in a list to see if they are beside each other - Allaye - Aug-25-2019 number = [2, 3, 4, 9, 8, 5, 1] def checkList(List1): for i in range(len(List1) - 1): #dont understand what you want me to do here if List1[i] == 9 and List1[i + 1] == 9: return True return False checkList(number)i dont get any error when i run the code but i also dont get any output RE: Checking number in a list to see if they are beside each other - Yoriz - Aug-25-2019 You don't print the result and the list does't have any 9 next to each other so it will return False. See Never use "for i in range(len(sequence)):" RE: Checking number in a list to see if they are beside each other - Malt - Aug-26-2019 (Aug-25-2019, 10:13 PM)Allaye Wrote:number = [2, 3, 4, 9, 8, 5, 1] def checkList(List1): for i in range(len(List1) - 1): #dont understand what you want me to do here if List1[i] == 9 and List1[i + 1] == 9: return True return False checkList(number)i dont get any error when i run the code but i also dont get any output You've to print the output instead if returning it, to see the output RE: Checking number in a list to see if they are beside each other - perfringo - Aug-26-2019 I suggest to follow naming conventions set in PEP8 - Function and Variable Names. Alternative approach could be using zip, set method .difference and built-in function any. This way in process of writing code one can learn much more of Python . zip is for creating pairs of consecutive numbers without need to worry about IndexError: >>> nums = [1, 9, 9, 2] >>> list(zip(nums, nums[1:])) [(1, 9), (9, 9), (9, 2)]set method .difference can be used for determining whether elements in pair are equal and match desired value: >>> set((1, 9)).difference((9,)) {1} >>> set((9, 9)).difference((9,)) set()In Python empty means False, so to get True if there is empty set one will use 'not'. any() is built-in function with short circuiting behaviour. Therefore function could be written (keyword argument is for cases when we need check numbers other than 9): >>> def check_list(nums, sentinel=9): ... return any(not set(pair).difference((sentinel,)) for pair in zip(nums, nums[1:])) ... >>> check_list([1, 2, 3]) False >>> check_list([1, 9, 9]) True >>> check_list([1, 2, 2], sentinel=2) TrueOne can use built-in help in interactive interpreter to learn about functions and methods used above ( help(zip) , help(set.difference) , help(any)
|