python - Remove all lists of multiple sets where the specific index is same -


object: given multiple sets of 2d arrays, remove items in each array share common value in specific index in of arrays.

the data large, around 20 groups, 200k arrays each, of large amounts of data in each array.

what know 2 sets

a = [['a',10,11],['b',10,11],['c',10,11]] b = [['e',10,11],['a',12,11],['f',10,11]] c = [['aa',10,11],['b',10,11],['cc',10,11]]  first = [] second = []  in range(len(a)):     first.append(a[i][0]) in range(len(b)):     second.append(b[i][0])  aa = set(map(tuple,first)) bb = set(map(tuple,second))  print aa print bb print bb.symmetric_difference(aa) 

i can new set contains unique items based on first index of each item(array) in array. however, remove items each set exist in other sets, not between two, , not make new set.

if can index of items in a match items in b or c, can delete them. loops, seems inefficient (not vectorized?).

given:

a = [['a',10,11],['b',10,11],['c',10,11]] b = [['e',10,11],['a',12,11],['f',10,11]] c = [['aa',10,11],['b',10,11],['cc',10,11]] 

the output be

a = [['c',10,11]] b = [['e',10,11],['f',10,11]] c = [['aa',10,11],['cc',10,11]] 

you can use counter dict count of how many times each first element appears in each sublist keep sublists have first element appears once.

from collections import counter  # count how many times each sublists first element appears in sublists keys = counter((ele[0] sub in (a,b,c) ele in sub)) l in (a, b, c):     sub in reversed(l):         # if count not 1 not unique remove         if keys[sub[0]] != 1:             l.remove(sub)  print (a) print(b) print(c) 

output:

[['c', 10, 11]] [['e', 10, 11], ['f', 10, 11]] [['aa', 10, 11], ['cc', 10, 11]] 

if want keep original lists append new lists instead of removing original.

if had sublist c first element end empty list:

a = [['a',10,11],['b',10,11],['c',10,11],["c",3,4]] b = [['e',10,11],['a',12,11],['f',10,11]] c = [['aa',10,11],['b',10,11],['cc',10,11]] 

output:

[] [['e', 10, 11], ['f', 10, 11]] [['aa', 10, 11], ['cc', 10, 11]] 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -