Hi i need to show the data of an matrix in anti-sense spiral starting in the center of the matrix, instead of going to the rigth and up i need to go rigth and down this is the matrix
n=5
matrix = []
for i in range(n):
matrix.append([0]*n)
values ='13,64,6,12,11,80,45,21,3,18,12,16,21,0,1,2,15,10,9,6,8,7,6,5,4'
values = values.split(",")
k = 0
for i in range(n-1, -1, -1):
for j in range(n-1, -1, -1):
matrix[i][j] = int(values[k])
k += 1
antihorario = []
x, y = int((n-1)/2), int((n-1)/2)
i= x
serie=1
de=0
impar=1
while serie <= n*n:
for j in range (y,y+1):
#matrix[i][j]=serie
antihorario.append(matrix[i][j])
serie +=1
l=j
for i in range(x-de,(x-1)-impar, -1):
j=l+1
antihorario.append(matrix[i][j-de])
serie +=1
y=j-de
k=i
for j in range (y-1,(k-1),-1):
i=k
antihorario.append(matrix[i][j])
serie +=1
l=j
for i in range (l+1,x+2):
j=l
antihorario.append(matrix[i][j])
serie +=1
y=j
k=i
for j in range(y+1, i+1):
i=k
antihorario.append(matrix[i][j])
serie +=1
l=j
y=l+1
x=k
impar+=2
de=1
print("Los nĂºmeros de la matriz en sentido antihorario son:", antihorario)
this is what i got: 21, 16, 15, 10, 9, 0, 3, 21, 45, 80, 12, 2, 8, 7, 6, 5, 4, 6, 1, 18, 11, 12, 6, 64, 13
this is what i expect: 21, 0, 3, 21, 45, 16, 15, 10, 9, 6, 1, 18, 11, 12, 6, 64, 13, 80, 12, 2, 8, 7, 6, 5, 4
Related
I need to multiply the number in each tuple, not the order i[0] * j[0] and i[1] * j[1], but i[0] * i[0], i[0] * j[1], i[0] * j[2] and so on.
Moreover, I need to add the number as well, such as i[0] + i[0], i[0] + j[1], i[0] + j[2] and so on.
Is there an easy way to do this, instead of my code below that needs a lot of for?
dice1 = (1, 2, 3, 4)
dice2 = (1, 2, 3, 4, 5, 6, 7, 8)
dice3 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
dice4 = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
dice5 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
dice6 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
myList = []
comp = []
d = 0
e = 0
for i in dice1:
for j in dice2:
d = i * j
myList.append(d)
e = len(myList)
comp.append(e)
You can utilize the itertools product function as follows:
from itertools import product
dice1 = (1, 2, 3, 4)
dice2 = (1, 2, 3, 4, 5, 6, 7, 8)
dice3 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
dice4 = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
dice5 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
dice6 = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
myList = []
comp = []
myList = [k[0] * k[1] for k in product(dice1, dice2)]
comp.append(len(myList)
I want to create a graph using VTK class https://lorensen.github.io/VTKExamples/site/Python/Graphs/ConstructGraph/
def main():
g = vtk.vtkMutableUndirectedGraph()
v1 = g.AddVertex()
v2 = g.AddVertex()
g.AddEdge ( v1, v2 )
print ('Number of vertices:', g.GetNumberOfVertices())
print ("Number of edges:", g.GetNumberOfEdges())
g.AddEdge ( v1, v2 )
print ('Number of vertices:', g.GetNumberOfVertices())
print ('Number of edges:', g.GetNumberOfEdges())
graphLayoutView = vtk.vtkGraphLayoutView()
graphLayoutView.AddRepresentationFromInput(g)
graphLayoutView.ResetCamera()
graphLayoutView.Render()
graphLayoutView.GetInteractor().Start()
is the function defined. But I not quite sure how to use this.
For instance, in networkx I would do
t = [0, 2, 4, 5, 6, 8, 9, 11, 13, 1, 10, 1, 3, 7]
h= [1, 3, 3, 1, 7, 7, 10, 12, 12, 14, 14, 12, 14, 10]
ed_ls = [(x, y) for x, y in zip(tail, head)]
G = nx.OrderedGraph()
G.add_edges_from(ed_ls)
nx.draw(G)
Suggestions on how to create the same graph in VTK will be helpful
You already found the correct API. Just have to use it. Something like
g = vtk.vtkMutableUndirectedGraph()
# add your 15 vertices
for i in range(15):
g.AddVertex()
t = [0, 2, 4, 5, 6, 8, 9, 11, 13, 1, 10, 1, 3, 7]
h= [1, 3, 3, 1, 7, 7, 10, 12, 12, 14, 14, 12, 14, 10]
for start, end in zip(t,h):
g.AddEdge( start, end )
def quicksort(array):
print(array)
n = 0
pivot = len(array) - 1
while n < pivot:
if array[pivot] > array[n]:
n+=1
elif array[pivot] <= array[n]:
array[n],array[pivot-1] = array[pivot-1], array[n]
array[pivot],array[pivot-1] = array[pivot-1], array[pivot]
pivot -= 1
if len(array[:pivot]) >1:
array[:pivot] = quicksort(array[:pivot])
if len(array[pivot+1:])> 1:
array[pivot+1:] = quicksort(array[pivot+1:])
return array
test = [21, 4, 1, 3, 9, 20, 25, 6, 21, 14]
print(quicksort(test))
raises the following error:
Error: Traceback (most recent call last):
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\idle.py", line 18, in <module>
print(quicksort(test))
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\idle.py", line 14, in quicksort
array[pivot:] = quicksort(array[pivot:])
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\idle.py", line 14, in quicksort
array[pivot:] = quicksort(array[pivot:])
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\idle.py", line 12, in quicksort
array[:pivot] = quicksort(array[:pivot])
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\idle.py", line 14, in quicksort
array[pivot:] = quicksort(array[pivot:])
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\idle.py", line 14, in quicksort
array[pivot:] = quicksort(array[pivot:])
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\idle.py", line 14, in quicksort
array[pivot:] = quicksort(array[pivot:])
[Previous line repeated 987 more times]
File "C:\Users\admin\AppData\Local\Programs\Python\Python37-32\idle.py", line 3, in quicksort
pivot = len(array) - 1
RecursionError: maximum recursion depth exceeded while calling a Python object
def quicksort(array):
print(array)
n = 0
pivot = len(array) - 1
while n < pivot:
if array[pivot] > array[n]:
n+=1
elif array[pivot] <= array[n]:
array[n],array[pivot-1] = array[pivot-1], array[n]
array[pivot],array[pivot-1] = array[pivot-1], array[pivot]
pivot -= 1
if len(array[:pivot]) >1:
array[:pivot] = quicksort(array[:pivot])
if len(array[pivot+1:])> 1:
array[pivot+1:] = quicksort(array[pivot+1:])
return array
test = [21, 4, 1, 3, 9, 20, 25, 6, 21, 14]
print(quicksort(test))
It was just excluding pivot from the subarrays. So array[pivot:] becomes array[pivot+1: ]
def quicksort(array):
print(array)
n = 0
pivot = len(array) - 1
while n < pivot:
if array[pivot] > array[n]:
n+=1
elif array[pivot] <= array[n]:
array[n],array[pivot-1] = array[pivot-1], array[n]
array[pivot],array[pivot-1] = array[pivot-1], array[pivot]
pivot -= 1
if len(array[:pivot]) >1:
array[:pivot] = quicksort(array[:pivot])
if len(array[pivot:])> 1:
array[pivot:] = quicksort(array[pivot:])
return array
test = [19, 4, 1, 3, 9, 20, 25, 6, 21, 14]
print(quicksort(test))
M Oehm is correct! I tried this modified code and it works on array with distinct elements. The output is:
[19, 4, 1, 3, 9, 20, 25, 6, 21, 14]
[6, 4, 1, 3, 9]
[6, 4, 1, 3]
[3, 4, 6]
[3, 4]
[14, 25, 20, 21, 19]
[19, 20, 21, 25]
[19, 20, 21]
[19, 20]
[1, 3, 4, 6, 9, 14, 19, 20, 21, 25]
If you need a quick sort that works on array with repeated elements, you will have to partition it into 3 (instead of 2) subarrays {{Xi}, {Xk}, {Xj}}, where {Xi} is smaller than pivot, {xk}=pivot and {Xj} > pivot.
Is there a replacement for the range command that can be used in for loop to add the numbers between 1 and 20 to a list.
Simple for loop
i = 1
l = []
while i <= 20:
l.append(i)
i = i + 1
Outputs
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
You can use np.linspace(1, 20, num=20)
If we are to avoid using a builtin as crucial as range, we might as well have some fun doing so.
l = []
while len(l) < 20:
l.append(len(l) + 1)
print(l)
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
I have frequency of each bigrams of a dataset.I need to sort it by descending order and visualise the top n bigrams.This is my frequency associated with each bigrams
{('best', 'price'): 95, ('price', 'range'): 190, ('range', 'got'): 5, ('got', 'diwali'): 2, ('diwali', 'sale'): 2, ('sale', 'simply'): 1, ('simply', 'amazed'): 1, ('amazed', 'performance'): 1, ('performance', 'camera'): 30, ('camera', 'clarity'): 35, ('clarity', 'device'): 1, ('device', 'speed'): 1, ('speed', 'looks'): 1, ('looks', 'display'): 1, ('display', 'everything'): 2, ('everything', 'nice'): 5, ('nice', 'heats'): 2, ('heats', 'lot'): 14, ('lot', 'u'): 2, ('u', 'using'): 3, ('using', 'months'): 20, ('months', 'no'): 10, ('no', 'problems'): 8, ('problems', 'whatsoever'): 1, ('whatsoever', 'great'): 1
Can anyone help me visualise these bigrams?
If I understand you correctly, this is what you need
import seaborn as sns
bg_dict = {('best', 'price'): 95, ('price', 'range'): 190, ('range', 'got'): 5, ('got', 'diwali'): 2, ('diwali', 'sale'): 2, ('sale', 'simply'): 1,
('simply', 'amazed'): 1, ('amazed', 'performance'): 1, ('performance', 'camera'): 30, ('camera', 'clarity'): 35, ('clarity', 'device'): 1,
('device', 'speed'): 1, ('speed', 'looks'): 1, ('looks', 'display'): 1, ('display', 'everything'): 2, ('everything', 'nice'): 5, ('nice', 'heats'): 2, ('heats', 'lot'): 14,
('lot', 'u'): 2, ('u', 'using'): 3, ('using', 'months'): 20, ('months', 'no'): 10, ('no', 'problems'): 8, ('problems', 'whatsoever'): 1, ('whatsoever', 'great'): 1}
bg_dict_sorted = sorted(bg_dict.items(), key=lambda kv: kv[1], reverse=True)
bg, counts = list(zip(*bg_dict_sorted))
bg_str = list(map(lambda x: '-'.join(x), bg))
sns.barplot(bg_str, counts)