比较1列2D数组并删除重复的Python [英] Compare 1 column of 2D array and remove duplicates Python
问题描述
array = [['abc',2,3,],
['abc',2,3],
['bb',5,5],
['bb',4,6],
['sa' 3,5],
['tt',2,1]]
删除第一列重复的任何行
即比较数组[0]并仅返回:
removeDups = [['sa',3,5],
['tt',2,1]]
我认为它应该是这样的:
(将第一个col设置为tmp变量,将tmp与剩余值进行比较,并从比较返回的#set数组)
($)
$ b $删除= compare(array,tmpCol)
array = copy.deepcopy(已删除)
打印repr(len(已删除))#testing
/ pre>
其中compare是:
(将每个剩余的数组项与tmp进行比较,如果匹配删除else返回原始数组)
def compare(valid,tmpCol):
for x in range(len )):
如果有效[x] [0]!= tmpCol:
del valid [x]
返回有效
else:
返回有效
我不断得到索引超出范围的错误。我尝试过其他方法,但我真的很感激一些帮助!
与其他答案类似,但使用字典而不是导入计数器:
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b $ $ $ $ $ $ $ $ $ $ $ $
#,如果需要,初始值为0
计数[elem [0]] = counts.get(elem [0],0)+ 1
new_array = []
对于数组中的elem:
#检查这个元素只有1个实例。
如果计数[elem [0]] == 1:
new_array.append(elem)
Say I have a 2D array like:
array = [['abc',2,3,],
['abc',2,3],
['bb',5,5],
['bb',4,6],
['sa',3,5],
['tt',2,1]]
I want to remove any rows where the first column duplicates
ie compare array[0] and return only:
removeDups = [['sa',3,5],
['tt',2,1]]
I think it should be something like: (set first col as tmp variable, compare tmp with remaining and #set array as returned from compare)
for x in range(len(array)):
tmpCol = array[x][0]
del array[x]
removed = compare(array, tmpCol)
array = copy.deepcopy(removed)
print repr(len(removed)) #testing
where compare is: (compare first col of each remaining array items with tmp, if match remove else return original array)
def compare(valid, tmpCol):
for x in range(len(valid)):
if valid[x][0] != tmpCol:
del valid[x]
return valid
else:
return valid
I keep getting 'index out of range' error. I've tried other ways of doing this, but I would really appreciate some help!
Similar to other answers, but using a dictionary instead of importing counter:
counts = {}
for elem in array:
# add 1 to counts for this string, creating new element at this key
# with initial value of 0 if needed
counts[elem[0]] = counts.get(elem[0], 0) + 1
new_array = []
for elem in array:
# check that there's only 1 instance of this element.
if counts[elem[0]] == 1:
new_array.append(elem)
这篇关于比较1列2D数组并删除重复的Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!