Python:迭代时从列表中删除重复项

时间:2017-04-20 20:30:33

标签: python python-3.x

我有一个名为results的列表,我想摆脱列表中的重复项,预期的结果与我的结果不符,我检查了我的代码但是找不到有什么问题,发生了什么?非常感谢!

results = [[-4, -2, 6], [-4, -2, 6], [-4, -2, 6], [-4, -2, 6], [-4, -2, 
    6], [-4, -2, 6], [-4, 0, 4], [-4, 0, 4], [-4, 1, 3], [-4, 1, 3], [-4, 2, 
    2], [-4, 2, 2], [-4, 2, 2], [-2, -2, 4], [-2, -2, 4], [-2, -2, 4], [-2, 
    -2, 4], [-2, 0, 2], [-2, 0, 2], [-2, 0, 2], [-2, -2, 4], [-2, -2, 4], 
    [-2, 0, 2], [-2, 0, 2], [-2, 0, 2], [-2, 0, 2], [-2, 0, 2], [-2, 0, 2]]

i = 0
while i < len(results):  
    j = i+1
    while j < len(results):
        if(set(results[i]) == set(results[j])):
            results.remove(results[j])
        else:
            j = j+1
    i = i+1
print(results)

OUTPUT:
[[-4,-2,6],[-4,0,4],[-4,1,3],[-4,2,2],[-2,-2,4],[-2,-2,4],[-2,0,2]]

EXPECTED RESULT:
[[-4,-2,6],[-4,0,4],[-4,1,3],[-4,2,2],[-2,-2,4],[-2,0,2]]

更新:我明白了。这个代码的逻辑没有问题,但我在一个地方犯了一个简单的错误(对不起......我是新手)。我应该替换 方法&#34;删除&#34;通过方法&#34; del&#34;,因为我想删除具有指定索引的项目,如果使用&#34;删除&#34;,它总是删除显示在该值列表中的第一个。无论如何,谢谢大家!

For example:
myList = ['Apple', 'Banana', 'Carrot','Apple']
myList.remove(myList[3])
print(myList)
expected output:['Apple', 'Banana', 'Carrot']
actual output: ['Banana', 'Carrot', 'Apple']

myList = ['Apple', 'Banana', 'Carrot','Apple']
del (myList[3])
print(myList)
OUTPUT: ['Apple', 'Banana', 'Carrot']

解决我的问题:

### use "del" instead of "remove"
#results.remove(results[j])
del results[j]

Another simple test example similar to my original question:
results = [[-2, -2, 4], [-2, 0, 2], [-2, 0, 2], [-2, 0, 2], [-2, -2, 4]]
i = 0
while i < len(results):  
    j = i+1
    while j < len(results):
        print(results[i],results[j])
            if(set(results[i]) == set(results[j])):
                #would get incorrect answer with "replace"
                results.remove(results[j])
                #try "del" to get the correct answer
                #del (results[j]) 
    else:
        j = j+1
i = i+1
print(results)

4 个答案:

答案 0 :(得分:3)

感谢Remove duplicate sublists from a list

results = [[-4, -2, 6], [-4, -2, 6], [-4, -2, 6], [-4, -2, 6], [-4, -2, 
    6], [-4, -2, 6], [-4, 0, 4], [-4, 0, 4], [-4, 1, 3], [-4, 1, 3], [-4, 2, 
    2], [-4, 2, 2], [-4, 2, 2], [-2, -2, 4], [-2, -2, 4], [-2, -2, 4], [-2, 
    -2, 4], [-2, 0, 2], [-2, 0, 2], [-2, 0, 2], [-2, -2, 4], [-2, -2, 4], 
    [-2, 0, 2], [-2, 0, 2], [-2, 0, 2], [-2, 0, 2], [-2, 0, 2], [-2, 0, 2]]

results = [list(x) for x in set([tuple(x) for x in results])]
print (results)

打印:

[[-4, -2, 6], [-2, 0, 2], [-4, 1, 3], [-2, -2, 4], [-4, 2, 2], [-4, 0, 4]]

答案 1 :(得分:3)

以下循环将在对上反复迭代,允许您在迭代期间改变列表。

var storedPictures = [];

  function addPictures(x, y) {
    for(var folder = 1; folder <= x; folder++) {
     for(var picture = 1; picture <= y; picture++) {
        storedPictures.push(
         '<img ' +
         'src="/path/to/file/' + folder + '/' +
         picture + '.jpg">'
        );
     }
    //console.log(storedPictures); //print the result to console
   }
  }
  addPictures(2,20);

但是,是的,使用套装可能会更好。上述功能版本将是:

i = len(results) - 1
while i >= 1:  
    j = i - 1
    while j >= 0:
        if(set(results[i]) == set(results[j])):
            results.remove(results[j])
            i -= 1
        j -= 1
    i -= 1

答案 2 :(得分:2)

设置 - 如果一切。

将内部列表转换为集合以删除其中的重复元素。将所有这些集推入另一个集合以摆脱重复(子)集。最后,将所有内容转换回列表列表:

W/System.err: Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb38b1400: Failure in SSL library, usually a protocol error
W/System.err: error:100000f7:SSL routines:OPENSSL_internal:WRONG_VERSION_NUMBER (external/boringssl/src/ssl/tls_record.c:192 0xa769d1aa:0x00000000)

我指的是How can I create a Set of Sets in Python?以找出关于no_dups = [list(s) for s in set(frozenset(l) for l in results))] 的内容。您不能只将子列表转换为集合,因为外部集合需要不可变元素进行散列(并且常规集合是可变的)。当然,你可以通过使用map /元组来解决这个问题,正如其他答案所指出的那样。

答案 3 :(得分:1)

您可以set使用map作为:

my_uniques = set(map(tuple, results))
#                     ^ type-cast inner list to tuples
#                       because lists are not hashable and
#                       hence can't be used with set

其中my_uniques将是一组唯一的tuple个对象:

set([(-2, -2, 4), (-4, 0, 4), (-4, -2, 6), (-4, 2, 2), (-2, 0, 2), (-4, 1, 3)])

如果你想将它转换为列表列表(我认为没必要),你必须明确地做:

my_uniques  = list(map(list, my_uniques))