[英]Python: check if last value of list occurs more than once
Problem: 问题:
Write a program that will search through a list to see if the last value in the list occurs more than once.
编写一个程序,搜索列表以查看列表中的最后一个值是否出现多次。 If the last value occurs more than once, return true.
如果最后一个值出现多次,则返回true。 Otherwise, return false.
否则,返回false。 If the list is empty, return false.
如果列表为空,则返回false。
My Code: 我的代码:
def go(list1):
for i in range(0,len(list1)):
if list1[i] is list1[-1:]:
return True
else:
return False
print ( go( [-99,1,2,3,4,5,6,7,8,9,10,5] ) )
print ( go( [10,9,8,7,6,5,4,3,2,1,9] ) )
print ( go( [10,20,30,40,50,-11818,40,30,20,10] ) )
print ( go( [32767] ) )
print ( go( [7,7,7,7] ) )
print ( go( [9,10,-88,100,-555,1000] ) )
print ( go( [10,10,10,11,456,10,10] ) )
print ( go( [-111,1,2,3,9,11,20,30] ) )
print ( go( [9,8,7,6,5,4,3,2,0,-2,9,9] ) )
print ( go( [12,15,18,21,23,1000] ) )
print ( go( [250,19,17,15,13,11,10,9,6,3,2,1,250] ) )
print ( go( [] ) )
My Output: 我的输出:
False
False
False
False
False
False
False
False
False
False
False
False
Desired Output: 期望的输出:
True
True
True
False
True
False
True
False
True
False
True
False
What am I doing wrong? 我究竟做错了什么? Why am I getting False for all my outputs?
为什么我的所有输出都变为假?
You should not be using is
here, because is
does not check for equality of values , but instead for equality of references . 你不应该使用
is
在这里,因为is
不检查值的平等,而是为引用的平等。
Instead you can use arr[-1] in arr[:-1]
to see if the final value of your list is present anywhere else in the list. 相反,您可以
arr[-1] in arr[:-1]
使用arr[-1] in arr[:-1]
来查看列表的最终值是否存在于列表中的任何其他位置。
x = [-99,1,2,3,4,5,6,7,8,9,10,5]
def is_last_contained(arr):
return arr[-1] in arr[:-1]
print(is_last_contained(x))
Output: 输出:
True
There are a couple errors in your code as written: 您编写的代码中有几个错误:
range(0,len(list1))
should be range(0,len(list1)-1)
since you don't want to compare the last element against itself. range(0,len(list1))
应该是range(0,len(list1)-1)
因为你不想将最后一个元素与它自己进行比较。 list1[-1:]
should be list1[-1]
. list1[-1:]
应该是list1[-1]
。 The former selects a slice, the latter a single element. That being said, @chrisz's solution is more Pythonic and concise :) 话虽如此,@ chrisz的解决方案更加Pythonic和简洁:)
Your mistake is that you're trying to compare an element with a slice of list. 你的错误是你试图将一个元素与一个列表进行比较。 Another mistake is that you're using the keyword
is
instead of ==
. 另一个错误是,你正在使用的关键字
is
不是==
。 In order to check whether the last element exists more than once in the list, you need to check if the current element is equal with the last element and their indices are different (which means that there more than one elements with the same value). 为了检查列表中是否存在多个最后一个元素,您需要检查当前元素是否与最后一个元素相等且它们的索引是否不同(这意味着有多个具有相同值的元素)。 My recommendation below:
我的推荐如下:
def go(list1):
length = len(list1)
for i in range(0,length):
if list1[i]==list1[-1] and not i==length-1:
return True
else:
return False
print ( go( [-99,1,2,3,4,5,6,7,8,9,10,5] ) )
print ( go( [10,9,8,7,6,5,4,3,2,1,9] ) )
print ( go( [10,20,30,40,50,-11818,40,30,20,10] ) )
print ( go( [32767] ) )
print ( go( [7,7,7,7] ) )
print ( go( [9,10,-88,100,-555,1000] ) )
print ( go( [10,10,10,11,456,10,10] ) )
print ( go( [-111,1,2,3,9,11,20,30] ) )
print ( go( [9,8,7,6,5,4,3,2,0,-2,9,9] ) )
print ( go( [12,15,18,21,23,1000] ) )
print ( go( [250,19,17,15,13,11,10,9,6,3,2,1,250] ) )
print ( go( [] ) )
Output: 输出:
True
True
True
False
True
False
True
False
True
False
True
False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.