首页 文章

使用Python删除数组中的double值

提问于
浏览
3

我写的一个Python脚本有问题 . Shortinfo:

What I have: 由包含整数的数组组成的数组:

finalSubLines = [[0,44,52,52,57],[12,154,25,154],[41,42,43,43,74]]

What I want from this program: 迭代所有子数组,对它们进行排序,并删除双值(例如,子数组0中的52,子数组1中的154和子数组2中的43)

My script so far:

finalSubLines = [[0,44,52,52,57],[12,154,25,154],[41,42,43,43,74]]
print "\n"

print "list:",finalSubLines,"\n========================"

for i in range (0, len(finalSubLines)):
    finalSubLines[i].sort()
    print "iterate until index:",len(finalSubLines[i])-2
    print "before del-process:",finalSubLines[i]
    for j in range (0, len(finalSubLines[i])-2):
        print "j=",j
        if finalSubLines[i][j] == finalSubLines[i][j+1]:
            del finalSubLines[i][j]     
    print "after del-process:",finalSubLines[i]
    print "-------------"
print finalSubLines

This is what I get:

The Problem:

  • 我没有't understand why the range of the first for-loop is until len(finalSubLines) and not len(finalSubLines)-1. I tryed the latter, but then the last sub-array wasn'达成 .

  • 主要问题是第二个for循环赢得了't reach all elements (blue rectangles in image). That' s为什么不会删除值154(参见图中的红色矩形) . 为什么会这样?

也许有一种更简单的方法可以得到我需要的东西,但由于我对脚本很新,我不知道更好^^

3 回答

  • 3

    简单的方法是使用集合:

    [sorted(set(sub)) for sub in finalSubLines]
    

    演示:

    >>> finalSubLines = [[0,44,52,52,57],[12,154,25,154],[41,42,43,43,74]]
    >>> [sorted(set(sub)) for sub in finalSubLines]
    [[0, 44, 52, 57], [12, 25, 154], [41, 42, 43, 74]]
    

    您的循环没有考虑到通过删除项目,您的列表变得越来越短;什么曾经在索引 i + 1 移动到索引 i ,但你的循环正愉快地转向索引 i + 1 ,并且那里的值曾经位于 i + 2 . 因此,您跳过元素 .

    有关所发生情况的更详细说明,请参见Loop "Forgets" to Remove Some Items .

  • 0

    试试这个:

    tempList=[]
    finalSubLines = [[0,44,52,52,57],[12,154,25,154],[41,42,43,43,74]]
    for item in finalSubLines:
        tempList.append(sorted(set(sorted(item))))
    print tempList,
    
  • 1

    如果您确实要删除两次出现的元素:

    finalSubLines = [[0,44,52,52,57],[12,154,25,154],[41,42,43,43,74]]
    
    from collections import Counter
    counts = [Counter(sub) for sub in finalSubLines]
    
    print([sorted(k for k,v in c.iteritems() if v != 2) for c in counts])
    

相关问题