import numpy as np
minute = datetime.datetime(year,month,day,9,15,00,000000)
minute1 = minute + timedelta(minutes = 1)
main_array = np.array([[681985, minute , 2.0, 3.0], [70913, minute , 5.0, 6.0]])
temp_array = np.array([[681985, minute1 , 2.0, 3.0]])
main_array = np.concatenate((main_array, temp_array))
main_array
array([[681985, datetime.datetime(2020, 5, 7, 9, 15), 2.0, 3.0],
[70913, datetime.datetime(2020, 5, 7, 9, 15), 5.0, 6.0],
[681985, datetime.datetime(2020, 5, 7, 9, 16), 2.0, 3.0]],
dtype=object)
I tried assigning new value 234.7 to the slice but it does not work:
main_array[(main_array[:,0] == 681985) & (main_array[:,1] == minute)][0][2] = 234.7
Using [0][2] indexing along with boolean mask creates a copy and changes the content of the copy. In order to change the original array, you can change the indexing by moving 2 inside indexing to prevent creating a copy:
main_array[(main_array[:,0] == 681985) & (main_array[:,1] == minute), 2] = 234.7
output:
[[681985 datetime.datetime(2020, 5, 7, 9, 15) 234.7 3.0]
[70913 datetime.datetime(2020, 5, 7, 9, 15) 5.0 6.0]
[681985 datetime.datetime(2020, 5, 7, 9, 16) 2.0 3.0]]
Related
I have a numpy array which contains hours from 4 days:
s = np.array([0.0, 1.0, 2.0, 3.0, 4.0 ....96.0])
I want to create a datetime object from that.
I know that the first element is at timestamp 2021-03-21 00:00,
so:
start_date = datetime.datetime.strptime('2021-03-21 00:00', '%Y-%m-%d %H:%M')
How can I create a new array which contains datetimes, incremented by an hour from the s array.
Use timedelta to build your new array:
>>> import numpy as np
>>> from datetime import datetime, timedelta
>>> s = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 96.0])
>>> start_date = datetime.strptime('2021-03-21 00:00', '%Y-%m-%d %H:%M')
>>> [start_date + timedelta(hours=diff) for diff in s]
[datetime.datetime(2021, 3, 21, 0, 0), datetime.datetime(2021, 3, 21, 1, 0), datetime.datetime(2021, 3, 21, 2, 0), datetime.datetime(2021, 3, 21, 3, 0), datetime.datetime(2021, 3, 21, 4, 0), datetime.datetime(2021, 3, 25, 0, 0)]
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 )
I want to pick the indices of number 8 without knowing its position in the array.
a = np.arange(10)
You can use np.where like :
>>> import numpy as np
>>> a = np.array([1,4,8,2,6,7,9,8,7,8,8,9,1,0])
>>> a
array([1, 4, 8, 2, 6, 7, 9, 8, 7, 8, 8, 9, 1, 0])
>>> np.where(a==8)[0]
array([ 2, 7, 9, 10], dtype=int64)
I understand that tuples are immutable objects, however, I know tuples support indexing and slicing. Thus, if I have a tuple assigned to a variable, I can reassign the variable to a new tuple object and change the value at the desired index position.
When I attempt to do this using an index slice, I am getting returned a tuple containing multiple tuples. I understand why this is happening, because I am passing comma separated slices of the original tuple, but I can't figure out how (if possible) I can return a one-dimensional tuple with a single element changed when working with larger sets of data.
Example:
someNumbers = tuple(i for i in range(0, 20))
print(someNumbers)
someNumbers = someNumbers[:10], 2000, someNumbers[11:]
print(someNumbers)
Outputs the following:
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19)
((0, 1, 2, 3, 4, 5, 6, 7, 8, 9), 2000, (11, 12, 13, 14, 15, 16, 17, 18, 19))
Can I return a one-dimensional tuple and change only the desired index value?
Use concatenation:
someNumbers = someNumbers[:10] + (2000,) + someNumbers[11:]
You can use tuple concatenation:
someNumbers = tuple(i for i in range(0, 20))
print(someNumbers)
# (2000, ) to differentiate it from (2000) which is a number
someNumbers = someNumbers[:10]+ (2000,) + someNumbers[11:]
print(someNumbers)
Outputs:
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19)
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 2000, 11, 12, 13, 14, 15, 16, 17, 18, 19)