【问题标题】:Python: Functions and Lists?Python:函数和列表?
【发布时间】:2016-01-14 14:36:48
【问题描述】:

我有一个函数,当输入一个列表和该列表中的特定字符串时,从列表中删除该特定字符串的任何重复项。 (find_startfind_end 是确定某个字符串的第一个和最后一个位置的单独函数)

def remove_duplicates(sorted_list, item):
    i = 0
    real_list = []
    for x in range(len(sorted_list)-1):
        if(sorted_list[i] == item):
            a = find_start(sorted_list, item)
            b = find_end(sorted_list, item)
            real_list = real_list + [item]
            i = i+(b-a)
        else:
            real_list = real_list + [sorted_list[i]]
        i+=1
    return real_list

例如,remove_duplicates(['a','a','b','b','c','c'], 'a') 将返回 ['a','b','b','c','c']

我正在尝试定义另一个函数,每次迭代都在其中使用这个函数,就像这样

def remove_all_duplicates(sorted_list):
    i = 0
    list_tru = []
    for x in range(len(sorted_list)):
        list_tru = remove_duplicates(sorted_list, sorted_list[i])
        i+=1
    return list_tru

但是如果我输入remove_all(['a','a','b','b','c','c']),它会输出['a','a','b','b','c']。我做错了什么?

【问题讨论】:

  • 您每次通过循环重新分配list_tru。由于您没有在循环期间修改 sorted_list,因此您只是删除了最后一个元素的重复项。
  • 在你的第二种情况下,你想做list_tru = sorted_list并在你的for循环中list_tru = remove_duplicates(list_tru, sorted_list[i])
  • 为什么不直接测试sorted_list[i]是否在list_tru中,如果不在则添加?
  • 您的目标是消除列表中的重复项还是只是学习如何实现这样的算法?如果是前者,请考虑使用set()
  • Brian,这是使用remove_duplicates 消除列表中的所有重复项(仅删除特定重复项)

标签: python list function


【解决方案1】:
def remove_all_duplicates(L):
    # NOTE: this modifies L IN-PLACE. Tread carefully

    i = 1
    while i<len(L):
        if L[i] == L[i-1]:
            del(L[i])
            continue
        i += 1

用法:

In [88]: L = ['a','a','b','b','c','c']

In [89]: remove_all_duplicates(L)

In [90]: L
Out[90]: ['a', 'b', 'c']

【讨论】:

    【解决方案2】:

    在每次迭代中,您只需继续回到原始的sorted_list。我建议复制它,然后对该副本进行操作:

    def remove_all_duplicates(sorted_list):
        list_tru = sorted_list[:] # copy it
        for x in set(sorted_list): # just use a set
            list_tru = remove_duplicates(list_tru, x) # remove this character from your list
        return list_tru
    

    我还将排序后的列表变成了set,这样您就不会尝试多次删除同一个字母的重复项,并删除了不必要的i 计数器。

    当然,如果您真正想做的只是从已排序的字符串列表中删除重复项,并且您不依赖于您正在开发的算法,那就特别简单:

    new_list = sorted(set(old_list))
    

    【讨论】:

    • 这有效,但前提是输入列表是['a','a','b','b','c','c']。如果我有一个像['a','b','b','c','c'] 这样的输入,它将输出['a','b'] 或者如果我输入['a','b','b','b','c','c'] 我得到一个索引错误。
    • 我应该把new_list 行放在哪里?
    • @hystehrichal - 带有new_list =... 的那一行可以替换您发布的所有代码。
    【解决方案3】:
    def remove_duplicates(sorted_list):
       for item in sorted_list:
           hits = sorted_list.count(item)
           while hits > 1:
               sorted_list.remove(item)
               hits = sorted_list.count(item)
       return sorted_list
    print(remove_duplicates(["a","a", "b", "b"]))
    

    这是我在现场能想到的最简单的方法,使用 .count 来判断是否有重复返回 ["a", "b"]

    【讨论】:

      【解决方案4】:

      你也可以用这个:

      A = ['a','a','b','c','c']            #example of input list with duplicates
      value = remove_duplicates(A)         #pass the list to the function
      print value                          #prints ['a','b','c']
      
      def remove_duplicates(A):
         B = []                            #empty list
         for item in A:
           if item in B:
             pass
           else:
             B.append(item)               #Append the list 
         return B
      

      希望这会有所帮助。祝你有美好的一天。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-10
        • 2015-04-22
        • 2014-11-17
        • 2013-06-30
        • 2014-04-01
        • 1970-01-01
        相关资源
        最近更新 更多