简体   繁体   English

python 最后一个数字重复不止一次

[英]python last number repeat more than once

My problem is that i dont get the desired output which i want.我的问题是我没有得到我想要的输出。 The list numbers can change as well as the size of the list.列表编号和列表大小都可以更改。

Task: Complete the method so that it will search through the list passed as a parameter to see if the last value in the list occurs more than once.任务:完成该方法,以便它搜索作为参数传递的列表,以查看列表中的最后一个值是否出现多次。 If the last value occurs more than once, return True (don't print "True").如果最后一个值出现多次,则返回 True(不打印“True”)。 Otherwise, return False (don't print "False").否则,返回 False(不打印“False”)。 If the list is empty, return False.如果列表为空,则返回 False。 Call your method from the main method, send it a list, and print the result.从主方法调用您的方法,向其发送一个列表,并打印结果。

def last_repeats(list):
  last_number = list[-1]
  lenght = len(list)
  for x in range (0,lenght-1):
    if list[x] == last_number:
      return True
    else:
      return False
def main():
  list = [0,0,5,5]
  print(last_repeats(list))


############################################
### Don't edit anything below this line. ###
############################################

if __name__ == '__main__':
  main()

Output I need:我需要的输出:

True

Output i get:我得到的输出:

False

You are returning after checking the first one, even though it might be false.您在检查第一个后返回,即使它可能是假的。 Consider your array [0, 0, 5, 5].考虑您的数组 [0, 0, 5, 5]。 the first element 0 != 5, so it immediately returns False.第一个元素 0 != 5,所以它立即返回 False。

To fix that, return False after the function ends要解决此问题,请在函数结束后返回 False

def last_repeats(lis): # 'list' is a keyword in python
  if len(lis) == 0: return False # Task requirement
  last_number = lis[-1]
  length = len(list) # typo?
  for x in range(length): # range(0, n) == range(n)
    if lis[x] == last_number:
      return True
  return False # So now you have checked every element in the list
def main():
  lis = [0, 0, 5, 5]
  print(last_repeats(lis))


############################################
### Don't edit anything below this line. ###
############################################

if __name__ == '__main__':
  main()

Also I just read that if the list is empty, it should return False.另外我刚刚读到,如果列表为空,它应该返回 False。 Currently, the code gives "IndexError".目前,代码给出了“IndexError”。 Try to fix it yourself!尝试自己修复它!

Have fun!玩得开心!

The range of values for range(start, stop, \\[step\\]) is determined by the formula r[i] = start + step*i where i >= 0 and r[i] < stop . range(start, stop, \\[step\\])的值range(start, stop, \\[step\\])由公式r[i] = start + step*i ,其中i >= 0r[i] < stop So for x in range(0, lenght - 1) will go until the second to last item.所以for x in range(0, lenght - 1)将一直到倒数第二个项目。 It should be range(0, lenght) :它应该是range(0, lenght)

def last_repeats(list):
  last_number = list[-1]
  lenght = len(list)
  vreturn = False
  for x in range (0,lenght):
    if list[x] == last_number:
      vreturn = True
  return vreturn

You get the False output because your if statement returns False on the first appearance of a number not equal to value of last_number (ie the first element in list) and that's because of the 'else' block which is not needed here.您得到 False 输出,因为您的 if 语句在不等于 last_number 值的数字(即列表中的第一个元素)第一次出现时返回 False,这是因为此处不需要“else”块。

You should change your if statement to just return True inside the loop.您应该将 if 语句更改为仅在循环内返回 True。 False returned outside the loop:在循环外返回 False:

for x in range (lenght):
    if list[x] == last_number:
      return True
    return False
def last_repeats(lis): # 'list' is a keyword in python
  last_number = lis[-1]
  length = len(list) # typo?
  for x in range(length): # range(0, n) == range(n)
    if lis[x] == last_number:
      return True
  return False # So now you have checked every element in the list
def main():
  lis = [0, 0, 5, 5]
  print(last_repeats(lis))


############################################
### Don't edit anything below this line. ###
############################################

if __name__ == '__main__':
  main()

you can use:您可以使用:

def last_repeats(my_list):
    return my_list and my_list[-1] in my_list[:-1]

def main():
    my_list = [0, 0, 5, 5]
    print(last_repeats(my_list))

############################################
### Don't edit anything below this line. ###
############################################

if __name__ == '__main__':
    main()

output:输出:

True

You need to put the return false statement outside the for loop.您需要将 return false 语句放在 for 循环之外。

def last_repeats(list):
  last_number = list[-1]
  lenght = len(list)
  for x in range (0,lenght-1):
    if list[x] == last_number:
      return True
  return False
def main():
  list = [0,0,5,5]
  print(last_repeats(list))


############################################
### Don't edit anything below this line. ###
############################################

if __name__ == '__main__':
  main()

Also you do not need the else case.你也不需要 else 情况。 This edit should achieve the purpose you are trying.此编辑应达到您尝试的目的。 At present your function returns false as soon as it inquires a non matching element.目前,只要查询不匹配的元素,您的函数就会返回 false。 Which should be only returned false after you have checked uptill second last element.只有在检查完倒数第二个元素后才应返回 false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM