Python Forum
Loop over an an array of array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loop over an an array of array
#1
I have an array of arrays I want to loop over to return two arrays called hills and valleys. When looping through each element, we check the item at index 1 that is element[0] if the value is equal to zero, we create another array and push the value inside. we keep a trace of it still when we meet another element where the element[0] is greater than zero then the trace breaks after that we push what is in the array to the valley array and the same thing goes for hills.
Here is an example

arr = [[0.0, 1564.0], [0.0, 1565.0], [0.0, 1566.0], [0.0, 1567.0], [0.0, 1568.0], [0.0, 1569.0], [0.0, 1570.0], [7.18, 1571.0], [0.0, 1572.0], [0.0, 1573.0], [7.33, 1574.0], [0.0, 1575.0], [0.0, 1576.0], [7.18, 1577.0], [0.0, 1578.0], [0.0, 1579.0], [0.0, 1580.0], [0.0, 1581.0], [12.28, 1582.0], [0.0, 1583.0], [4.26, 1584.0], [0.0, 1585.0]] 

should return hills [
[7.18, 1571.0],
 [7.33, 1574.0], 
 [7.18, 1577.0],
 [4.26, 1584.0]
] and

 valleys should be [
[[0.0, 1564.0], [0.0, 1565.0], [0.0, 1566.0], [0.0, 1567.0], [0.0, 1568.0], [0.0, 1569.0], [0.0, 1570.0]],
[[0.0, 1572.0], [0.0, 1573.0]], 
[[0.0, 1575.0], [0.0, 1576.0]], 
[[0.0, 1578.0], [0.0, 1579.0], [0.0, 1580.0], [0.0, 1581.0]], 
[[0.0, 1583.0]],
[[0.0, 1585.0]]
].

Here is what I have done so far 

def plot_curve_with_annotation(input_array):

    hills = []
    valleys = []

    for element in input_array:
        value = element[0]
        
        if value == 0.0 or value == None:
         valleys.append(element)
        elif value > 0.0:
         hills.append(element)

    return hills, valleys

hills, valleys = plot_curve_with_annotation(arr)
print("Hills:", len(hills))
print("Valleys:", len(valleys))
buran write Nov-28-2023, 05:02 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Do you have a question? Your code appears to do what you want.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 429 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 288 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,910 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  How Write Part of a Binary Array? Assembler 1 357 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  How to remove some elements from an array in python? gohanhango 9 1,208 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  IPython errors for numpy array min/max methods muelaner 1 573 Nov-04-2023, 09:22 PM
Last Post: snippsat
  Convert np Array A to networkx G IanAnderson 2 693 Jul-05-2023, 11:42 AM
Last Post: IanAnderson
  Help using a dynamic array excel formula with XLWings FXMonkey 2 1,300 Jun-06-2023, 09:46 PM
Last Post: FXMonkey
  [Newbie] Multiple Array azhuda 3 1,033 Jun-01-2023, 04:29 AM
Last Post: azhuda
  Pyserial Readline() conversion to array bprosman 1 1,919 Apr-11-2023, 12:44 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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