Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A more efficient code
#1
I just started with Python. I want to add the information of the array ‘toc’ to the array ‘toc_n.’ The first two columns of both ones are ‘year’ and ‘day of the year,’ but the array ‘toc’ lacks some days (i.e., fewer rows). I wrote the following code. Could you please suggest something more efficient/clever?

for i in range(2196):
for j in range(2173):
dd = np.logical_and(toc_n[i,0] == toc[j,0] , toc_n[i,1] == toc[j,1])
if dd == True:
toc_n[i,2]=toc[j,2]
buran write Oct-16-2023, 11:06 AM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply
#2
would numpy.concatenate work here?
Reply
#3
I don't think concatenate will work. An odd combination of matching and replacing makes me think that there is not a vectorized solution. In pandas you could make a date intex and merge, but that adds a column instead of replacing values in an existing column. The code below does he matching and replacement in a single pass. It does require that the arrays are sorted by date.
import numpy as np

# make two Nx3 arrays to work with.
toc_n = np.array([[a, b, 0] for a in range(1492, 2023) for b in range(1, 366)])
toc = np.array([[a, b, 1] for a in range(1492, 2023) for b in range(1, 366, 3)])

# Combine year and day into a date value.  If not sorted, sort arrays by
# this value.  Sorting is an expensive operation, but the numpy sort is
# efficient compared to looping in python.
toc_n = np.hstack((toc_n, (toc_n[:, 0]*365+toc_n[:, 1]).reshape(-1, 1)))
toc = np.hstack((toc, (toc[:, 0]*365+toc[:, 1]).reshape(-1, 1)))

# Loop through toc_n and toc, copying toc[2] to toc_n[2] when
# the dates match.  Since both arrays are sorted this can be
# done in a single pass.
try:
    toc_iter = iter(toc)
    t = next(toc_iter)
    for t_n in toc_n:
        while t[-1] < t_n[-1]:
            t = next(toc_iter)
        if t[-1] == t_n[-1]:
            t_n[2] = t[2]
            t = next(toc_iter)
except StopIteration:
    pass
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cleaning my code to make it more efficient BSDevo 13 1,381 Sep-27-2023, 10:39 PM
Last Post: BSDevo
  Making a function more efficient CatorCanulis 9 1,864 Oct-06-2022, 07:47 AM
Last Post: DPaul
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,820 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  I there a more efficient way of printing ? Capitaine_Flam 7 3,517 Dec-01-2020, 10:37 AM
Last Post: buran
  A more efficient way of comparing two images in a python vukan 0 2,034 Mar-17-2020, 11:39 AM
Last Post: vukan
  how to make iterative search more efficient renergy 2 2,282 Jan-03-2020, 03:43 PM
Last Post: stullis
  Simple problem. looking for an efficient way silverchicken24 3 2,344 Oct-14-2019, 07:13 PM
Last Post: Larz60+
  Most efficient way to define sub keys of a dictionary? wrybread 1 2,126 Feb-21-2019, 12:23 AM
Last Post: snippsat
  Efficient way of iterating a list of records anguis 4 3,062 Feb-19-2019, 03:39 AM
Last Post: scidam
  Is there a more efficient way to code this project? gkiranseo 1 2,692 Sep-25-2018, 09:51 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020