Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia lang remove duplicates in a list of tuples considering the second element

Tags:

tuples

julia

Is there an easy way to remove duplicates of a list of tuples where for the duplicates only the second element is considered? For example when I have the following list:

a = [("a",1),("b",3),("c",4),("d",8),("e",1)]

I want to end up with:

a = [("b",3),("c",4),("d",8),("e",1)]

I doesn't matter if I keep "a" or "e".

like image 628
riasc Avatar asked Jan 31 '26 05:01

riasc


1 Answers

Yes, you can use the unique(f, itr) method to do this; it returns the elements of itr where f returns unique values.

julia> unique(x->x[2], a)
4-element Array{Tuple{String,Int64},1}:
 ("a", 1)
 ("b", 3)
 ("c", 4)
 ("d", 8)
like image 120
mbauman Avatar answered Feb 03 '26 06:02

mbauman