Python 删除相邻重复元素后打印列表 new_list=[] i=0 def移除(nums): 全球i 而i

Python 删除相邻重复元素后打印列表 new_list=[] i=0 def移除(nums): 全球i 而i,python,recursion,Python,Recursion,问题:给定一个数字列表,返回一个列表,其中所有相邻==元素已减少为单个元素,因此[1,2,2,3]返回[1,2,3]。您可以创建新列表或修改传入的列表 问题:打印的最终列表由[1,2,3,5]组成,而不是[1,2,3,5,4,5,6,7,8,9,8]您想要的问题最好由 itertools.groupby所做的是,它通过生成元素的元组和作为列表的连续组将连续键分组在一起 为了清楚地理解itertools.groupby,您可以转储通过对连续数字列表进行分组而生成的元组结果列表 l Out[35]:

问题:给定一个数字列表,返回一个列表,其中所有相邻==元素已减少为单个元素,因此
[1,2,2,3]
返回
[1,2,3]
。您可以创建新列表或修改传入的列表


问题:打印的最终列表由
[1,2,3,5]
组成,而不是
[1,2,3,5,4,5,6,7,8,9,8]
您想要的问题最好由

itertools.groupby
所做的是,它通过生成元素的元组和作为列表的连续组将连续键分组在一起

为了清楚地理解
itertools.groupby
,您可以转储通过对连续数字列表进行分组而生成的元组结果列表

l
Out[35]: [1, 2, 3, 5, 4, 4, 5, 5, 6, 7, 7, 7, 8, 8, 9, 8, 8]

from itertools import groupby
[k for k, _ in groupby(l)]
Out[36]: [1, 2, 3, 5, 4, 5, 6, 7, 8, 9, 8]

你想要的是一个最好的解决方法

itertools.groupby
所做的是,它通过生成元素的元组和作为列表的连续组将连续键分组在一起

为了清楚地理解
itertools.groupby
,您可以转储通过对连续数字列表进行分组而生成的元组结果列表

l
Out[35]: [1, 2, 3, 5, 4, 4, 5, 5, 6, 7, 7, 7, 8, 8, 9, 8, 8]

from itertools import groupby
[k for k, _ in groupby(l)]
Out[36]: [1, 2, 3, 5, 4, 5, 6, 7, 8, 9, 8]

我创建了一个函数,它获取一个列表并迭代其项,然后添加与上一次迭代中已经添加到列表中的项不同的项

[1, 2, 3, 5, 4, 5, 6, 7, 8, 9, 8]

我创建了一个函数,它获取一个列表并迭代其项,然后添加与上一次迭代中已经添加到列表中的项不同的项

[1, 2, 3, 5, 4, 5, 6, 7, 8, 9, 8]

这是一个完美的发电机。我不会改变原来的名单。相反,我返回的是一个新列表,相邻值之间没有相等的值

l = [1, 2, 3, 5, 4, 4, 5, 5, 6, 7, 7, 7, 8, 8, 9, 8, 8]  # List that we want to change

def remove_adjacent(l):             # Define a new function and accept an argument: the list to check.
    new = [l[0]]                    # Make a new list (temporal) and assing like it's first item the first item of the main list. It's the same as new = [] and new.append(l[0]).
    for item in l[1:]:              # We iterate across the list, but we don't iterate on the first item because we've alreaday added it to the list, if you want you can delete the slice in [1:] since it will only make the iteration a really small fraction more slowly.
        if new[-1] != item:         # We check if the new item is the same as the last item added to the new list, if not, we add it.
            new.append(item)        # We add the item to the new list.
    return new                      # We return the list.

print(remove_adjacent(l))           # We check it.
# [1, 2, 3, 5, 4, 5, 6, 7, 8, 9, 8]

设置

def removerator(l):
    last = None
    for x in l:
        if x != last:
            last = x
            yield x

list(removerator(l))

[1, 2, 3, 5, 4, 5, 6, 7, 8, 9, 8]

这是一个完美的发电机。我不会改变原来的名单。相反,我返回的是一个新列表,相邻值之间没有相等的值

l = [1, 2, 3, 5, 4, 4, 5, 5, 6, 7, 7, 7, 8, 8, 9, 8, 8]  # List that we want to change

def remove_adjacent(l):             # Define a new function and accept an argument: the list to check.
    new = [l[0]]                    # Make a new list (temporal) and assing like it's first item the first item of the main list. It's the same as new = [] and new.append(l[0]).
    for item in l[1:]:              # We iterate across the list, but we don't iterate on the first item because we've alreaday added it to the list, if you want you can delete the slice in [1:] since it will only make the iteration a really small fraction more slowly.
        if new[-1] != item:         # We check if the new item is the same as the last item added to the new list, if not, we add it.
            new.append(item)        # We add the item to the new list.
    return new                      # We return the list.

print(remove_adjacent(l))           # We check it.
# [1, 2, 3, 5, 4, 5, 6, 7, 8, 9, 8]

设置

def removerator(l):
    last = None
    for x in l:
        if x != last:
            last = x
            yield x

list(removerator(l))

[1, 2, 3, 5, 4, 5, 6, 7, 8, 9, 8]

在循环中,打印一些变量以查看发生了什么。获取列表中相邻项对的一种简单方法是使用zip(nums,nums[1:]:print(a,b)-不知道这是否有帮助。当然,我不是在为您分配任务,但看起来您试图决定是使用循环还是递归,但最终您同时使用了这两种方法。(注:去掉全局变量。使用返回值。)@KennyOstrom我从未要求任何人做我的作业。在循环中,打印一些变量以查看发生了什么。获取列表中相邻项对的一种简单方法是
for a,b,In-zip(nums,nums[1:]:print(a,b)
-不知道这是否有帮助。当然,我不是在为你做作业,但看起来你在试图决定是使用循环还是递归,但你最终同时使用了这两种方法。(注:去掉全局变量。使用返回。)@KennyOstrom我从来没有要求任何人做我的作业。删除中的I=0工作起来很有魅力,我想写I=0会将I重新初始化为零,但我没有意识到这是递归调用的局部性!!@being\u弹性谢谢!请注意我附加了最后一项。检查这是否可以避免。谢谢!有任何问题吗我想我自己会发现,我运行循环的时间少了一次。I=0 inside remove\u nextant工作得很有魅力,我想写I=0会将I重新初始化为零,但我没有意识到这是递归调用的局部性!!@being\u弹性谢谢!请注意,我附加了最后一项。检查如果可以避免的话。谢谢!无论如何,我会自己弄明白,因为我少运行一次循环。