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
Big Grin [solved] how to associate value in an array? paul18fr 9 3,709 Aug-26-2025, 05:44 AM
Last Post: paul18fr
  Numpy, array(2d,bool), flipping regions. MvGulik 2 1,582 Oct-27-2024, 11:06 AM
Last Post: MvGulik
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 2,207 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  ValueError: could not broadcast input array from shape makingwithheld 1 5,364 Jul-06-2024, 03:02 PM
Last Post: paul18fr
  python code to calculate mean of an array of numbers using numpy viren 3 2,079 May-29-2024, 04:49 PM
Last Post: Gribouillis
  Writing a cycle to find the nearest point from the array Tysrusko 0 1,291 May-10-2024, 11:49 AM
Last Post: Tysrusko
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 4,734 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 1,864 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 12,688 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  How Write Part of a Binary Array? Assembler 1 1,435 Jan-14-2024, 11:35 PM
Last Post: Gribouillis

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.