Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

python - what is "1 in array[]" mean in Julia?

I'm in a situation where I have to rewrite some Julia code into Python code and I cannot reproduce this line.

if 1 in array1[array2] || 1 in array1[array3]

In my understanding, this line comparing arrays array1 to array2 and array1 to array3, to see if the index array2 of array1 is 1 or the index array3 of array1 is 1.

So, I reproduced this code into python code with my understand,

for i, j in zip(array2, array3):
    if array1[i] == 1 or array1[j] == 1:

But this code didn't work like an above code and I got a ValueError like below:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I'm not sure if this is my misunderstanding of the Julia's line or my Python code is wrong.

Could someone tell me what is wrong?

[edit]: Here is Julia code of this problem. here I am using karate club network as an input matrix dir = "to/your/path" ln = "soc-karate.mtx"

mtx = MatrixMarketRead(string(dir,strip(ln)));
A = mtx - spdiagm(diag(mtx))
n = size(A,1);
A = speye(n) - A * spdiagm(1./vec(sum(A,1)));
println(A)
function findDegrees(Ac::SparseMatrixCSC)
  degrees = zeros(Int,length(Ac.colptr)-1)
    for i = 1:length(degrees)
      degrees[i] = Ac.colptr[i+1]-Ac.colptr[i]-1
    end
  return degrees
end

function lowDegreeNodes(A::SparseMatrixCSC,At::SparseMatrixCSC,d::Int64,dout::Vector,din::Vector)
  # 1: find low degree nodes
  n = size(A,1)
  U = collect(dout.==1)
  println(U)
  V = collect(din.==1)
  Z = min((dout+din) .>= 1 , (dout+din) .<= 8 )
  # 2:  visited = 0  ==> NotVisited
  #             = 1  ==> FNode
  #             = 2  ==> NotEliminated
  visited = zeros(length(U))
  
  for u = 1:n
    if Z[u]
      if visited[u] == 0
        Au = A.rowval[ A.colptr[u]:A.colptr[u+1]-1 ]
        Av = At.rowval[ At.colptr[u]:At.colptr[u+1]-1 ]
        if 1 in visited[Au] || 1 in visited[Av]
          visited[u] = 2
        else
          visited[Au] = 2
          visited[u] = 1
        end
      end
    end

    if V[u]
      if visited[u] == 0
        Au = A.rowval[ A.colptr[u]:A.colptr[u+1]-1 ]
        Av = At.rowval[ At.colptr[u]:At.colptr[u+1]-1 ]
        if 1 in visited[Au] || 1 in visited[Av]
          visited[u] = 2
        else
          visited[Av] = 2
          visited[u] = 1
        end
      end
    end

    if U[u]
      if visited[u] == 0
                Au = A.rowval[ A.colptr[u]:A.colptr[u+1]-1 ]

        Av = At.rowval[ At.colptr[u]:At.colptr[u+1]-1 ]
        if 1 in visited[Au] || 1 in visited[Av]
          visited[u] = 2
        else
          visited[Av] = 2
          visited[u] = 1
        end
      end
    end
  end
  return visited .== 1
end
dout = findDegrees(A)
din = findDegrees(A')
z = lowDegreeNodes(A, A', 3, dout, din)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
if 1 in array1[array2] || 1 in array1[array3]

In my understanding, this line comparing arrays array1 to array2 and array1 to array3, to see if the index array2 of array1 is 1 or the index array3 of array1 is 1.

I don't think that's correct. I think this line tests if 1 is in the values of array1 at indices array2 or indices array3. Let me make a MWE:

julia> array2 = [2, 3]
2-element Array{Int64,1}:
 2
 3

julia> array3 = [4, 5]
2-element Array{Int64,1}:
 4
 5

julia> array1 = [1, 9, 1, 2, 3]
5-element Array{Int64,1}:
 1
 9
 1
 2
 3

julia> 1 in array1[array2] || 1 in array1[array3]
true

julia> array1 = [1, 9, 4, 2, 3] # now only at the 1st position is there a 1
5-element Array{Int64,1}:
 1
 9
 4
 2
 3

julia> 1 in array1[array2] || 1 in array1[array3]
false

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...