How to check symmetry inside tuples and avoide overwrtting?

Is there any easy way to check if you have symmetric tuples in your lists?

Like auto checking that [(2,3),(3,5),(3,2),..., [10,8)] has. Because of (2,3) and (3,2). Also I don’r have any tuples with same number as first and second elements

“Easy” in what sense? You can write a simple function that does exactly that, for example:

julia> function bad_tups(tups)
           for i in firstindex(tups):lastindex(tups)
               tups[i][1] == tups[i][2] && return true
               for j in i+1:lastindex(tups)
                   if (tups[i][1] == tups[j][2] && tups[i][2] == tups[j][1])
                       return true
                   end
               end
           end
           return false
       end
bad_tups (generic function with 1 method)

julia> tups = [(2,3),(3,5),(3,2)]
3-element Vector{Tuple{Int64, Int64}}:
 (2, 3)
 (3, 5)
 (3, 2)

julia> bad_tups(tups)
true

Thank @lmiq indeed I have a dictionary that maps some ids to some list of tuples. And having symmetic tuples in the values cause some problem. I’m looking for a way to loop trough all the values in the dictionary and then check the values, if they had symmetric. I want to reduce the values to all symetric tuples. For example:

# Letter is a vectors of string 
# numbers is a dictionary mapping some numers to some vector of tuples of (int64,int64)


sym_tup = Dict()
for n in letters
    for (i,j) in numbers[n]
        if (j,i) in numbers[n]
            sym_tup = [(j,i)]
        end
    end
end

This overwrite the values of symmetric tuples. How to avide that?

It is better if you provide an example of the type of collection you want, and the result expected. For instance, the first proposal there does not work for your case because the dictionary is not indexable, and that was misleading in your original post. So you will get better advice by being very specific in what you have and what you want as a result.

julia> x = [(2, 3), (3, 5), (3, 2), (10, 8)]
4-element Vector{Tuple{Int64, Int64}}:
 (2, 3)
 (3, 5)
 (3, 2)
 (10, 8)

julia> intersect(x, reverse.(x))
2-element Vector{Tuple{Int64, Int64}}:
 (2, 3)
 (3, 2)
1 Like

try this way

for n in letter
    for (i,j) in numbers[n]
        if (j,i) in numbers[n]
            push!(get!(()->[],sym_tup,n), (i,j))
        end
    end
end

It’s not meant to be a “solution” to the problem, just an answer to how to make this piece of code work, at least as far as I understand from the comments ###

# Letter is a vectors of string 
# numbers is a dictionary mapping some numers to some vector of tuples of (int64,int64)
1 Like

i tried the new 1.9.0 version of julia with this script, but i get the following strange screens.
And the for loop doesn’t finish until I “kill” it with ctrl-z

letter=string.(Set(rand(10:20, 5)))
numbers=Dict{String, Vector{Tuple{Int64,Int64}}}()


t=filter(e->-(e...)!=0,[(rand(1:5),rand(1:5)) for _ in 1:100])

[numbers[l]=unique(t[rand(1:length(t),10)])  for l in letter]

sym_tup = Dict()

for n in letter
    for (i,j) in numbers[n]
        if (j,i) in numbers[n]
            push!(get!(()->[],sym_tup,n), (i,j))
        end
    end
end

#87[numbers[l]=unique(t[rand(1:length(t),10)])  for l in letter]    
4-element Vector{Vector{Tuple{Int64, Int64}}}:
 [(3, 5), (2, 5), (2, 3), (5, 4), (1, 5), (1, 2), (3, 1), (2, 4), (3, 4), (4, 1)]
 [(4, 5), (4, 2), (3, 4), (3, 2), (1, 5), (2, 3), (1, 3), (2, 5)]   
 [(1, 5), (5, 4), (4, 2), (3, 4), (3, 5), (4, 5), (5, 2), (5, 3), (2, 1)]
 [(1, 4), (4, 5), (4, 1), (2, 5), (4, 2), (2, 3), (1, 2)]

#87sym_tup = Dict()
Dict{Any, Any}()

#87for n in letter

this is my system

julia> versioninfo()
Julia Version 1.9.0
Commit 8e63055292 (2023-05-07 11:25 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: 8 × 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, tigerlake)
  Threads: 4 on 8 virtual cores
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 4