How do i split this array into more arrays at each dash?

['A- 0.5', 'WC- 0.5', 'WC- 0.0', 'n- 0.0', 'WC- 0.3333333333333333', 'ABC- 1.7777777777777777', 'DEF- 1.0', 'GHI- 1.5']

i need it so i can sort by the number

I don’t know what “split array into more arrays” means, but here’s how you can sort them by their number.

array = ['A- 0.5', 'WC- 0.5', 'WC- 0.0', 'n- 0.0', 'WC- 0.3333333333333333', 'ABC- 1.7777777777777777', 'DEF- 1.0', 'GHI- 1.5']

def sort_key(element):
    return float(element.split("-")[1].strip())

sorted_array = sorted(array, key=sort_key)

Explanation:

element.split("-") splits each string by dashes, so "A- 0.5" becomes ["A", " 0.5"]. Then take the index 1 for the number part. Then strip() removes surrounding spaces. Then float() converts it to a number.

1 Like
your_data = ...
filtered = [ float( t.split("-",1)[1] ) for t in your_data]

Will it work for your data?