0

I am making up a code in Python 3.7 in which I need to split an array in chunks.

The array is something similar to the following one:

['1.60500002', '1.61500001', '1.625', '1.63499999','NO',
'1.73500001','1.745', 'NO','2.04500008', '2.05499983']

I am interested to create n different slices (3 in this case) everytime the string 'NO' occurs. Then the output array should be something like:

[['1.60500002', '1.61500001', '1.625', '1.63499999'],
['1.73500001', '1.745'],
['2.04500008', '2.05499983']]

Could someone help me?

4
  • 1
    Please share what you’ve tried so far and where you’re getting stuck. Commented May 7, 2020 at 17:30
  • 2
    You can use itertools groupby, try this answer, replacing '.' with 'NO': stackoverflow.com/a/47604467/12684122 Commented May 7, 2020 at 17:36
  • 2
    Also, you can use list comprehenssion, with [i.strip().split(' ') for i in ' '.join(_list).split('NO') if len(i) > 0 ] Commented May 7, 2020 at 17:38
  • 1
    Does this answer your question? How to split a list-of-strings into sublists-of-strings by a specific string element Commented May 7, 2020 at 18:08

4 Answers 4

1

You can try iterating through the list, checking if the element is a floating point number and if so adding it to a list and if not creating a new list. Just add up all the lists you've made and that should be it.

def split_str(inp):
    array_of_array = []
    arr = []
    for a in inp:
        try:
            float(a)
            arr.append(a)
        except Exception:
            array_of_array.append(arr)
            arr = []
    array_of_array.append(arr)
    return array_of_array
Sign up to request clarification or add additional context in comments.

Comments

0

loop over the array and if the element is "NO", we split the array until the "NO" element and add it to the output. if we reached the end, the last part would be from the last "NO" to the end.

my_array = ['1.60500002', '1.61500001', '1.625', '1.63499999','NO','1.73500001', '1.745', 'NO','2.04500008', '2.05499983']
    output = []
    counter = 0
    start = 0
    for item in my_array:
        if item == "NO":
            output.append(my_array[start:counter])
            start = counter + 1
        if counter == len(my_array) - 1:
            output.append(my_array[start:counter + 1])
        counter += 1

2 Comments

loop over the array and if the element is "NO", we split the array until the "NO" element and add it to the output. if we reached the end, the last part would be from the last "NO" to the end.
Please edit your question and put the explanation in the answer. Thanks!
0

Since the only split indicator is 'NO' this should be easier. All you have to do is check for 'NO' and then create a new list. You also have to handle for the first element scenario as it can be a 'NO' or a number but creation of a new list if it is 'NO' is not required

To create a new list inside a list, you can do examplelist.append([])

Then to access a specific list inside a list, you can mention the list number in square brackets before using append(). eg. examplelist[list_number].append(whatever)

Here is the code I came up with :

#Input
array = ['1.60500002', '1.61500001', '1.625', '1.63499999','NO','1.73500001', '1.745', 'NO','2.04500008', '2.05499983']
#Declaring
result = [] #empty list
list_number = 0 #to access a specific list inside a list
starting_element = True #var to handle starting scenario
for element in array:
    #starting scenario
    if starting_element:
        result.append([])
        if element != 'NO':
            result[list_number].append(element)
        starting_element = False
    #NO scenario
    elif element == 'NO':
        list_number += 1
        result.append([])
    #Number scenario
    elif element != 'NO':
        result[list_number].append(element)

print(result)

Comments

0

A simple solution is to loop through the list appending to a list then appending that list to the result and clearing that list when you encounter the split string.

def split_list(strings: list, split: str = "\s"):
    result = []

    split_list = []
    for string in strings:
        if string == split:
            result.append(split_list.copy())
            split_list.clear()
        else:
            split_list.append(string)
    
    if split_list: result.append(split_list.copy())

    return result


print(
    split_list(
        [
            "1.60500002",
            "1.61500001",
            "1.625",
            "1.63499999",
            "NO",
            "1.73500001",
            "1.745",
            "NO",
            "2.04500008",
            "2.05499983",
        ],
        "NO",
    )
)

Output:

[['1.60500002', '1.61500001', '1.625', '1.63499999'], ['1.73500001', '1.745'], ['2.04500008', '2.05499983']]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.