简体   繁体   English

Python:检查列表的最后一个值是否出现多次

[英]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: 您编写的代码中有几个错误:

  1. 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)因为你不想将最后一个元素与它自己进行比较。
  2. 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.

相关问题 Python Pandas检查一个值是否在同一天发生了一次以上 - Python Pandas check if a value occurs more then once in the same day 如何找到一个单词(字符串)是否在 python 的输入/列表中出现多次 - How can I find if a word (string) occurs more than once in an input/list in python python 最后一个数字重复不止一次 - python last number repeat more than once 在Python 2.6中是否有任何函数可以找到列表的模式(如果最多出现一个,则为多个)? - Is there any function to find the mode (or multiple if more than 1 value occurs most) of a list in Python 2.6? 如何确定列表中是否有任何值超过两次? - How to determine if any value occurs more than twice in a list? 检查 object_id 是否在 queryset.annotate Case When 参数中出现多次 - Check if object_id occurs more than once in queryset.annotate Case When parameter python数据帧时间序列检查值在最后n行中的变化是否超过x并转发n行 - python dataframe timeseries check if value changed more than x in last n rows and forward n rows 如何检查某个字符串是否在列表中重复多次 - How to check if a certain string repeats more than once in a list 如果最大的数字在同一个列表中多次出现,我如何将它们全部打印出来? - If the largest number occurs more than once in the same list, how do I print them all out? 替换一次多次出现的字符串中的模式 - Replace a pattern in a string once which occurs more than once
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM