Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,如何检查列表中的最后几个元素是否相等?_Python_List - Fatal编程技术网

在python中,如何检查列表中的最后几个元素是否相等?

在python中,如何检查列表中的最后几个元素是否相等?,python,list,Python,List,我将一个接一个地(使用循环)将整数添加到列表中,如下所示: A.append(x)其中x是一个整数,它最终给出例如: A = [4, 8, 2, 4, 3, 7, 7, 7] occurrences = collections.Counter(A) if occurrences[A[-1]] >= 3: raise Exception("Lots of the latest integers are similar") 在每次循环期间,即在每个整数添加到数组末

我将一个接一个地(使用循环)将整数添加到列表中,如下所示:

A.append(x)
其中x是一个整数,它最终给出例如:

A = [4, 8, 2, 4, 3, 7, 7, 7]
occurrences = collections.Counter(A)
if occurrences[A[-1]] >= 3:
   raise Exception("Lots of the latest integers are similar")
在每次循环期间,即在每个整数添加到数组末尾之后,我想检查同一个整数是否添加了一定次数(例如,下面的示例中为3次),如果是,则抛出异常

伪代码:

if somewayofcheckingA == 3:
    raise Exception("Lots of the latest integers are similar")
我可以做下面的事情,但是如果我想检查,比如说,100次重复,那么显然代码会变得一团糟

if A[-1] == A[-2] and A[-2] == A[-3]:
    raise Exception("Lots of the latest integers are similar")

谢谢

将列表传递给
set()
将返回一个包含列表中所有唯一值的集合。您可以使用切片表示法获得最后
n
值的列表,方法如下

n = 3
if len(A) >= n and len(set(A[-n:])) == 1:
    raise Exception("Lots of the latest integers are similar")

将列表传递给
set()
将返回一个包含列表中所有唯一值的集合。您可以使用切片表示法获得最后
n
值的列表,方法如下

n = 3
if len(A) >= n and len(set(A[-n:])) == 1:
    raise Exception("Lots of the latest integers are similar")

如果你只想检查最后3个,那么这个就可以了

limit = 3
if len(set(A[-limit:])) == 1:
    raise Exception("Lots of the latest integers are similar")

如果你只想检查最后3个,那么这个就可以了

limit = 3
if len(set(A[-limit:])) == 1:
    raise Exception("Lots of the latest integers are similar")

可以使用collections.Counter()计算最后一个元素出现的次数。 例如:

A = [4, 8, 2, 4, 3, 7, 7, 7]
occurrences = collections.Counter(A)
if occurrences[A[-1]] >= 3:
   raise Exception("Lots of the latest integers are similar")
或者更简单的方法

if A.count(A[-1]) >= 3:
   raise Exception("Lots of the latest integers are similar")

**此代码检查最后一个元素是否出现在列表的任何其他索引中

您可以使用collections.Counter()计算最后一个元素出现的次数。 例如:

A = [4, 8, 2, 4, 3, 7, 7, 7]
occurrences = collections.Counter(A)
if occurrences[A[-1]] >= 3:
   raise Exception("Lots of the latest integers are similar")
或者更简单的方法

if A.count(A[-1]) >= 3:
   raise Exception("Lots of the latest integers are similar")

**这段代码检查最后一个元素是否出现在列表的任何其他索引中。我认为这是某种蟒蛇

class CustomList(list):
    def __init__(self, seq=()):
        self.last_equal_items = []
        super().__init__(seq)

    def append(self, some_item):
        if self.last_equal_items and some_item != self.last_equal_items[-1]:
            self.last_equal_items = []
        self.last_equal_items.append(some_item)
        if len(self.last_equal_items) >= 3:
            raise ValueError("Last equal items larger that 3")
        else:
            super(CustomList, self).append(some_item)

test = CustomList([])
test.append(1)
test.append(1)
test.append(1)
test.append(2)
test.append(1)
print(test)

您可以像使用
list
一样使用
CustomList
。当你插入第三个相等的值时,它会发出警报。

我为你做了这件事。我认为这是某种蟒蛇

class CustomList(list):
    def __init__(self, seq=()):
        self.last_equal_items = []
        super().__init__(seq)

    def append(self, some_item):
        if self.last_equal_items and some_item != self.last_equal_items[-1]:
            self.last_equal_items = []
        self.last_equal_items.append(some_item)
        if len(self.last_equal_items) >= 3:
            raise ValueError("Last equal items larger that 3")
        else:
            super(CustomList, self).append(some_item)

test = CustomList([])
test.append(1)
test.append(1)
test.append(1)
test.append(2)
test.append(1)
print(test)
lists = [1,4,3,3];
def somewayofcheckingA(lists, a):
    lists.reverse()
    i = 0
    k = lists[0]
    count = 0
    while i < a:
        if(lists[i] == k):
            count= count+1
        i = i+1
    return count
        
print(test(lists, 3))
您可以像使用
list
一样使用
CustomList
。当您插入第三个相等的值时,它将发出警报。

lists=[1,4,3,3];
lists = [1,4,3,3];
def somewayofcheckingA(lists, a):
    lists.reverse()
    i = 0
    k = lists[0]
    count = 0
    while i < a:
        if(lists[i] == k):
            count= count+1
        i = i+1
    return count
        
print(test(lists, 3))
def以某种方式检查a(列表,a): list.reverse() i=0 k=列表[0] 计数=0 而我
其中lists是列表,a是要检查的次数

这个答案很容易理解,并且使用了基本的循环和条件语句,我认为在尝试其他建议的解决方案之前,您应该掌握这些语句,这些解决方案更具python风格,但您可能会在混合中迷失方向。

list=[1,4,3,3];
def以某种方式检查a(列表,a):
list.reverse()
i=0
k=列表[0]
计数=0
而我
其中lists是列表,a是要检查的次数



这个答案很容易理解,并且使用了基本的循环和条件语句,我认为在尝试其他建议的解决方案之前,您应该掌握这些语句,这些解决方案更像python,但您可能会在混合中迷失方向。

如何定义“最近附加的整数”?其中有多少?如何定义“最近附加的整数”?他们中有多少人?你的答案和@Iain ShelvingtonI写的答案一样,不知道答案是否已经发布。你的答案和@Iain ShelvingtonI写的答案一样,不知道答案是否已经发布。你打败了我。除此之外,我打算建议在提取最近的项目后,例如,
recent=A[-n::
,然后评估
len(recent)-len(set(recent))
。然后,您可以区分集合中的元素不多,因为原始列表中的项目少于
n
,而集合中的元素不多,因为存在重复项。@alaniwi
len(A)>=n
的附加条件可能有效是的,足够公平。通过计算长度差异,即使您没有完整的
n
项,也可以计算重复项的数量,但问题显然没有详细说明要求,以说明是否应该这样做。如果有两组重复项,每个重复项的元素少于n,则会引发误报,但是加在一起就不止n个了。@DYZ你能举个例子说明你在说什么吗?问题中没有提到多套副本你比我抢先一步。除此之外,我打算建议在提取最近的项目后,例如,
recent=A[-n::
,然后评估
len(recent)-len(set(recent))
。然后,您可以区分集合中的元素不多,因为原始列表中的项目少于
n
,而集合中的元素不多,因为存在重复项。@alaniwi
len(A)>=n
的附加条件可能有效是的,足够公平。通过计算长度差异,即使您没有完整的
n
项,也可以计算重复项的数量,但问题显然没有详细说明要求,以说明是否应该这样做。如果有两组重复项,每个重复项的元素少于n,则会引发误报,但是加在一起就不止n个了。@DYZ你能举个例子说明你在说什么吗?问题中没有提到多组重复项。您的代码不适用于此
A=[7,4,8,2,4,3,7,7]
因此不应引发任何异常,但您的代码将出现错误。这些解决方案中的任何一个都会在大型列表中执行不良,这两种方法都会导致对整个列表进行迭代。您的代码不适用于此
A=[7,4,8,2,4,3,7,7]
,因此不应引发任何异常,但您的代码会出现错误。这两种解决方案在大型列表上的性能都很差