Grouping by the first two characters of the parameter

Hello,

I have a list of room names as below

P1.01
P1.02…
P2.01
P2.02…
P3.01
P3.02…

How can I group a list by the first two characters?
Trying to get list with P1, P2, P3 …

Thank you

Look at the node List.GroupByKey.

hello!

Is the list ordered? And if not, do you want it ordered through this process?

Is it case sensitive?

You will first need to extract the “prefix” then group by key

1 Like

For funsies, a python solution.

# The input list to be grouped
input_list = IN[0]

def group_list_by_first_two_characters(input_list):
    # Create an empty dictionary to store the grouped lists
    grouped_lists = {}

    # Iterate over each item in the input list
    for item in input_list:
        # Extract the first two characters of the string
        key = item[:2]

        # If the key is not already in the dictionary, create a new list
        if key not in grouped_lists:
            grouped_lists[key] = []

        # Append the item to the corresponding list based on the key
        grouped_lists[key].append(item)

    # Convert the dictionary values (grouped lists) into a list of lists
    output_list = list(grouped_lists.values())

    return output_list

# Call the grouping function with the input list
output_list = group_list_by_first_two_characters(input_list)

# Return the grouped list as output
OUT = output_list
1 Like

Hello Daniel,

thank you for your answer. May I ask you, if I will have the number of parameters:

P1.1.01
P1.1.02
P2.1.01
P2.1.02
P2.2.01
P2.2.02
P3.1.01
P3.1.02…

and I want ro group the same charachters before second dot?

Result:
P1.1
P2.1
P2.2
P3.1

There’s a few ways
You could still split with “.” and then concatonate index 0 and 1
You could also use a substring and remove the 4 first character but then you need to make sure you dont have P10.1

Daniel is correct.

He has suggested two solutions, but what you really need to think about is what data you are handling as a whole using a more human logic.

What i mean by that is, Grouping lists based “The first two characters” is different than “Deliminating a list from a period & removing everything before it” which is different again from, “here is how my data is structured and this is how i want it handled”

Try to think about why the data is coming as it is and use that as your logic for making a solution
ie: if in this scenario “P2.2.02” it could be…

P2 = Building Name
2 = Building Level or zone
02 = Number in sequence
“.” = Deliminator to distinguish boundries between sections

Now with that in mind, you might say;

I want my output to split the buildings into different lists and then group the building levels with their respective rooms.

Or

I want group my list by building Name & level

Or

I want to itentify all the rooms in the different buildings.

But now you see, that these would all be different solutions with regard to grouping or logic. but in this way you itentify to yourself what you are actually achieveing and therefore it gives more context to what variable data patterns that data could face (Mentioned above by dan with regarding to string splitting with 4 characters but then realising your building has more than 10 levels or 10 zones or whatever those characters represent)

Hello pyXam,

your example is accurate.I want group my list by building Name & level.

Great, so now analyse your data as a whole.

Do you have basements? Could this throw out your numbering?

Are the floors zoned? Needing another number?

Do the building levels have more than 10 levels or an “R” level that could throw out your numbering?

Do any of the rooms have options? (A or B)

Do the rooms of differing types need to be bunched in any way?

Is the room name or room number the sorting factor for how you want to read it at the end? For presumably a schedule?

Are there other factors such as common plantrooms which are relevant to all 3 buildings?

Are there common spaces that sequentially arnt next to each other numbering wise but you need them grouped?

Ps: im not asking so i can build this for you, i am helping you to understand your own requirements so you can begin building this :slight_smile:

Of course we here can help but i know i at least will be spending the next 4 hours or so with my kids so best you make a start :stuck_out_tongue: