简体   繁体   English

检查列表中的相邻元素

[英]Check adjacent elements in list

def remove_adjacent(nums):
  list1 = []
  le = len(nums) # get length of input array (le-1 is the last index)
  for idx in range(len(nums)): # iterate through nums using the indices
      if idx < le-1: # if before the last element
          if nums[idx] != nums[idx +1]: # and if current element is not the same as the next element
              list1.append(nums[idx]) # append the current element. Otherwise (elem reoccurs), do not append!
      else: # if at last elem of nums
          list1.append(nums[idx]) # just append

  return list1

I am learning Python (currently doing the Google course).我正在学习 Python(目前正在学习 Google 课程)。 The task was:任务是:

D. Given a list of numbers, return a list where all adjacent == elements have been reduced to a single element, so [1, 2, 2, 3] returns [1, 2, 3]. D. 给定一个数字列表,返回一个列表,其中所有相邻的 == 元素都已简化为单个元素,因此 [1, 2, 2, 3] 返回 [1, 2, 3]。 You may create a new list or modify the passed in list.您可以创建一个新列表或修改传入的列表。

Now I think I managed to solve the task, but it looks complicated.现在我想我设法解决了这个任务,但它看起来很复杂。 Furthermore, I want to learn to code in the most basic way first, without using tons of module imports.此外,我想先学习以最基本的方式编码,而不是使用大量的模块导入。 How could this be solved in a more elegant way using the basic set of methods?如何使用基本方法集以更优雅的方式解决这个问题? Also, I would love advice and tips on good coding style, habits, form etc.此外,我希望获得有关良好编码风格、习惯、形式等方面的建议和技巧。

You can make a set with your list.你可以用你的清单做一套。 Sets can only have single instances of an element.集合只能有一个元素的单个实例。

my_list = [1,2,2,3]
my_set = set(my_list)

print(s)
>>> {1,2,3}

edit: Make the set into a list.编辑:将集合设为列表。

my_list = list(my_set)

I tried to put it in the most simple way.我试着用最简单的方式来表达。

l = [1,2,2,2,2,3,3]
def removeAdj(num):
   prevelem = num[0] -1 ##Assigning prev value one less than first element so that it will not be same as first element
   updatedList = []  ## New list
   for elem in num:
      if elem !=prevelem:
          updatedList.append(elem) ## If element not same as previous element append
      prevelem = elem ## Assign previous value the current value
   return updatedList ## Return updated list
print(removeAdj(l))

Your function seems to work correctly but it could be more "pythonic" by iterating through the elements directly instead of using indices.您的 function 似乎工作正常,但通过直接迭代元素而不是使用索引可能更“pythonic”。

Without using any "fancy" standard library functions:不使用任何“花哨”的标准库函数:

def filter_adjacent_dupes(lst):
     # make sure to check if `lst` is empty
     if not lst:
         return lst

     # construct a new list that doesn't contain adjacent duplicates
     filtered = [lst[0]]
     for elem in lst[1:]:
         if elem != filtered[-1]:
             filtered.append(elem)
     return filtered

You might also consider turning the above function into a generator:你也可以考虑把上面的 function 变成一个生成器:

def filter_adjacent_dupes(lst):
     # make sure to check if `lst` is empty
     if not lst:
         return

     # yield each valid candidate, skipping duplicates
     yield lst[0]
     prev_value = lst[0]
     for elem in lst[1:]:
         if elem != prev_value:
             yield elem
         prev_value = elem

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

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