Python Forum
splitting numeric list based on condition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
splitting numeric list based on condition
#6
thanks a lot for the explanation and the tip to print out the results. will try it as well

I would like to extend this problem to another level where once these sublists are created. I need to iterate through this sublist and extract the index whenever there is a break in sequence. I tried to do that myself but I am not able to come up with a good solution for it. I know it will be easier to create a separate function and to pass the results of this separated arrays to is but beyond that I am not sure how best to proceed. As an example I have the array as below:

array = [0,1,2,3,4,5,6,7,8,9,10,0,1,2,3,4,5,6,7,3,8,9,10]

and with your help, I achieved the following:
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [0, 1, 2, 3, 4, 5, 6, 7, 3, 8, 9, 10]]

Now I would like to loop into this sublists and when it find a break in the sequence within the sub list it will extract the index and value of it. in this case the break of sequence occurs in the second sublist from 7 to 3. I tried something like below which resulted in TypeError: append() takes exactly one argument (0 given)

The expected output in this case is 3

arrays = [[]] # array of sub-arrays
        
for i, num in enumerate(array):          
    arrays[-1].append(num)                  
    if num == 10 and i != len(array)-1:  
        arrays.append([]) 
print(arrays)

seq_break = []
for i in arrays:
    if i[-1] != i[-1] + 1: #checks to see if the next value in the sublist equals to the previous value +1 which means a sequence, if not extract the break value
        seq_break.append()
print(seq_break)
    
Reply


Messages In This Thread
RE: splitting numeric list based on condition - by python_newbie09 - May-26-2019, 06:42 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Numeric Enigma Machine idev 9 650 Mar-29-2024, 06:15 PM
Last Post: idev
  unable to remove all elements from list based on a condition sg_python 3 501 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Sent email based on if condition stewietopg 1 905 Mar-15-2023, 08:54 AM
Last Post: menator01
  create new column based on condition arvin 12 2,362 Dec-13-2022, 04:53 PM
Last Post: jefsummers
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 877 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,559 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Splitting strings in list of strings jesse68 3 1,823 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
Question Numeric Anagrams - Count Occurances monty024 2 1,532 Nov-13-2021, 05:05 PM
Last Post: monty024
  How to get datetime from numeric format field klllmmm 3 2,034 Nov-06-2021, 03:26 PM
Last Post: snippsat
  How to map two data frames based on multiple condition SriRajesh 0 1,518 Oct-27-2021, 02:43 PM
Last Post: SriRajesh

Forum Jump:

User Panel Messages

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