El nino portable
El nino portable

Reputation: 21

How to sort python lists due to certain criteria

I would like to sort a list or an array using python to achive the following: Say my initial list is:

example_list = ["retg_1_gertg","fsvs_1_vs","vrtv_2_srtv","srtv_2_bzt","wft_3_btb","tvsrt_3_rtbbrz"]

I would like to get all the elements that have 1 behind the first underscore together in one list and the ones that have 2 together in one list and so on. So the result should be:

sorted_list = [["retg_1_gertg","fsvs_1_vs"],["vrtv_2_srtv","srtv_2_bzt"],["wft_3_btb","tvsrt_3_rtbbrz"]]

My code:

import numpy as np
import string

example_list = ["retg_1_gertg","fsvs_1_vs","vrtv_2_srtv","srtv_2_bzt","wft_3_btb","tvsrt_3_rtbbrz"]

def sort_list(imagelist):

    # get number of wafers
    waferlist = []
    for image in imagelist:
        wafer_id = string.split(image,"_")[1]
        waferlist.append(wafer_id)
    waferlist = set(waferlist)
    waferlist = list(waferlist)
    number_of_wafers = len(waferlist)
    # create list
    sorted_list = []
    for i in range(number_of_wafers):
        sorted_list.append([])
    for i in range(number_of_wafers):
        wafer_id = waferlist[i]
        for image in imagelist:
            if string.split(image,"_")[1] == wafer_id:
                sorted_list[i].append(image)
    return sorted_list

sorted_list = sort_list(example_list)

works but it is really awkward and it involves many for loops that slow down everything if the lists are large.

Is there any more elegant way using numpy or anything?

Help is appreciated. Thanks.

Upvotes: 1

Views: 35

Answers (1)

phormalitize
phormalitize

Reputation: 91

I'm not sure how much more elegant this solution is; it is a bit more efficient. You could first sort the list and then go through and filter into final set of sorted lists:

example_list = ["retg_1_gertg","fsvs_1_vs","vrtv_2_srtv","srtv_2_bzt","wft_3_btb","tvsrt_3_rtbbrz"]

sorted_list = sorted(example_list, key=lambda x: x[x.index('_')+1])

result = [[]]
current_num = sorted_list[0][sorted_list[0].index('_')+1]
index = 0

for i in example_list:
    if current_num != i[i.index('_')+1]:
       current_num = i[i.index('_')+1]
       index += 1
       result.append([])
    result[index].append(i)

print result

If you can make assumptions about the values after the first underscore character, you could clean it up a bit (for example, if you knew that they would always be sequential numbers starting at 1).

Upvotes: 2

Related Questions