Check array to find if they are the same - python

I am new to programming in Python. I had used Matlab extensively. I want to check if in a 2D array,any two arrays are same. I have points lets say (x,y,z) coordinates in the format
data=[[x1,y1,z1],[x2,y2,z2]...[xn,yn,zn]].
I want to check if any point is equal to another point. And if there are any similar instance, i want to know how many are same and which two are same. The code i have in Python is something like
data = [[1,2,3],[3,1,8],[4,2,5],[1,2,3]]
s=0
for i in range(len(data)):
a=data[i]
for j in range(len(data)):
while i != j:
if data[j]==a:
s=s+1
c=i
d=j
print(s)
print(c,d)
and i also dont want the answer that point N is equal to point N etc
I also have used
for j in range(i,len(R)):
but it also dont give the correct answer.

This working well:
data =[[1,2,3],[4,5,6],[2,5,6],[0,9,1],[1,5,7],[2,5,4]]
st=[]
dic={}
while len(data)>1:
d = data[0]
data.remove(d)
st=[]
for dat in data:
k=0
for i in d:
if i in dat:
k=k+1
if k>1:
st.append(dat)
if len(st)>0:
dic[str(d)]=st
for key in dic.keys():
print(key," : ", dic[key])

Apparently, found a workthrough myself
d=[[1,2,3],[3,5,4],[5,6,9],[0,2,1],[1,2,3]]
s=0
for i in range(len(d)):
for j in range(i+1,len(d)):
if d[i]==d[j]:
s=s+1
x=i
y=j
print(s)
print(x,y)

Related

Can any one explain me this question Consider the searching problem

Can any one explain me this Question?
I am not understanding what it is asking me to do!
Question:
Consider the searching problem.
Input: a sequence of n numbers A = [A1,A2,...An] and a value v.
Output: an index i such that v = A[i] or the special value NIL if v does not appear in A.
Write pseudo-code for linear search, which scans the sequence, looking for v. Using a loop invariant fulfilling the three necessary properties.
My Answer:
a = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
n = int(input("Enter a number :"))
if n in a:
print(f"{n} is in the sequence")
else:
print("NIL")
Is this answer correct?
You must work with the indexes of the list so the answer might be a code like:
A = [11,10,0,13,6,25,9,17,2,19]
v = 6
for i in range(len(A)):
if A[i] == v:
print(i)
break
else:
print("NIL")
For this example the answer will be 4 because the number that is given from the input is 6 and we can find that number ( v ) at the 4th index of our list ( A ).
Hope that was helpful.

How to avoid counting numbers when do calculation in a for loop in python

I am working on a python algorithm. I have built a for loop to generate the numbers i want. I am going to use these numbers as indices to generate a string called constraint_sty3.
Here is a part of my code:
def constraint():
val=4
ind=[]
for i in range(1,7):
for j in range(val,val+i):
val = val+1
ind.append(j)
if(i < 6):
boundNode.insert(j, 'x%d' % (j-(2*i+1)))
constraint_sty3 = str(boundNode[(j+1])]
My problem is when calculating j+1 , it appears it is not doing the right calculation but doing the counting at the same time. Like a j=j+1. All the other j ,i -related str(boundNode[])like str(boundNode[j]) work well but only this contains j+1 return a problematic result.
You have an error on the last line of your code:
constraint_sty3 = str(boundNode[(j+1])]
it should be:
constraint_sty3 = str(boundNode[(j+1)])
But i dont think this is the error
But by putting (j+1) inside brackets [ ] it will count plus one on that array/list
You should do the calculation before and then append it.

Getting interior elements of a 2D array only

I have a matrix like the below:
test_numpy_array = np.array([[2,3,4,5],
[6,4,1,2],
[3,4,5,1],
[8,9,7,6],
[4,1,2,5]])
How do I extract only the interior numbers, such that they have 8 neighbors for an array of any size?
This should work:
test_numpy_array[1:-1, 1:-1]
If you're looking for help regarding determination of whether each element is a local minimum, you should probably edit your original post to reflect that.
Regardless, here's some code to push you in the right direction for that. Not sure if you wanted to print, or get a 2D boolean area or what, but this should get you close enough.
def is_local_min(a, r, c):
for i in range(r-1, r+2):
for j in range(c-1, c+2):
if i == r and j == c:
continue
if a[i][j] < a[r][c]:
return False
return True
def get_local_mins(arr):
local_mins = []
for i in range(1, np.shape(arr)[0]-1):
for j in range(1, np.shape(arr)[1]-1):
if is_local_min(arr, i, j):
local_mins.append((i, j))
return local_mins

2d Array Python :list index out of range

I am new to Python programming and I am trying to write code to check if a DFA( Deterministic Finite Automata) has empty language or not.
I am using 2-d array to store state of the DFA.
On executing the code I keep getting list index out of range. How can I fix this?
Below is the code
top=-1
count=0
n=int(input("\nEnter the no of states"))
mat=[[]]
b=[]
print("\nEnter the transition table")
for i in range(0,n):
for j in range(0,n):
mat.append(input())
finalState=input("\nEnter the final state:")
startState=input("\nEnter the start state:")
for i in range(0,n):
for j in range(0,n):
if mat[i][j]:
b[++top]=j
for k in range(top):
if b[k]==finalState:
count+=1
if count>0:
print("\nLanguage is not empty")
else:
print("\nLanguage is empty")
when you make a 2x2 table, you want mat to be [[1,2],[3,4]], but you're getting [[],1,2,3,4] right now.
Instead, try:
mat = []
for i in range(n):
row = []
for j in range(n):
row.append(input())
mat.append(row)
Also, Python does not have a "++" operator, so b[++top]=j is the same as b[top] = j. If you want to increment top, you have to do that on its own line before you use it to index the list.
On top of that, b has zero elements, so indexing it in any way will cause a crash. If you're trying to increase the size of b by adding new items to it, use append. Then you don't need a top variable at all.
b.append(j)

looping through the rows of a matrix in sage

I am trying to program a Graham-Schmidt process in sage and cannot figure out how to loop through the rows of an array.
def graham_schmidt(W):
a=0
U=W
for i in W.dims()[0]:# this is the not working part
print w
a=a+1
for j in xrange(0,-2):
a=a+1
U[i]=U[i]-(transpose(U[j])*w)/(transpose(U[j])*U[j])*U[j]
return a;
You're making things far too complicated. If W is not a sparse matrix, you can just do
for row in W:
Since you also need the row index, you can use Python's built-in enumerate:
for i, row in enumerate(W):
or (uglier)
for i in xrange(len(W.shape[0])):

Categories