1

I have a tuple as given below

all_combi= [
    ('a', 33.333333333333336),
    ('a', 38.333333333333336),
    ('a', 43.333333333333336),
    ('a', 48.333333333333336),
    ('a', 53.333333333333336),
    ('a', 58.333333333333336),
    ('a', 63.333333333333336),
    ('a', 68.33333333333334),
    ('a', 73.33333333333334),
    ('a', 78.33333333333334),
    ('a', 83.33333333333334),
    ('a', 88.33333333333334),
    ('a', 93.33333333333334),
    ('a', 98.33333333333334),
    ('b', 33.333333333333336),
    ('b', 38.333333333333336),
    ('b', 43.333333333333336),
    ('b', 48.333333333333336),
    ('b', 53.333333333333336),
    ('b', 58.333333333333336),
    ('b', 63.333333333333336),
    ('b', 68.33333333333334),
    ('b', 73.33333333333334),
    ('b', 78.33333333333334),
    ('b', 83.33333333333334),
    ('b', 88.33333333333334),
    ('b', 93.33333333333334),
    ('b', 98.33333333333334),
    ('c', 33.333333333333336),
    ('c', 38.333333333333336),
    ('c', 43.333333333333336),
    ('c', 48.333333333333336),
    ('c', 53.333333333333336),
    ('c', 58.333333333333336),
    ('c', 63.333333333333336),
    ('c', 68.33333333333334),
    ('c', 73.33333333333334),
    ('c', 78.33333333333334),
    ('c', 83.33333333333334),
    ('c', 88.33333333333334),
    ('c', 93.33333333333334),
    ('c', 98.33333333333334)]

I want to sort this tuple based on this list

instr_list. = ['a', 'b', 'c']

The sample of the expected output is given below

[[
    ('a', 33.333333333333336),
    ('b', 33.333333333333336),
    ('c', 33.333333333333336)
 ], [
    [('a', 33.333333333333336),
    ('b', 38.333333333333336),
    ('c', 43.333333333333336)]
 ]]

I tried the following solution given at here to sort a tuple based on list. But it doesnt give the desired outcome. I tried using explicit loop but it doesnt work. Any help is appreciated...

def get_slab_list(all_combi, instr_list):
    out_master_list = []
    for i in range(len(instr_list)):
        #k=0
        out_list=[]
        
        for j in all_combi:
            if j[0] == instr_list[i]:
                out_list.append(j[1])

        out_master_list.append(out_list)
    return out_master_list

sample = get_slab_list(all_combi, instr_list)
Sid
  • 552
  • 6
  • 21
  • If your goal is to sort based on the list ```['a', 'b', 'c']``` why does you expected output just sort on the second element of the tuple followed by the first element of the tuple? – itprorh66 May 16 '21 at 14:12
  • It looks like you want to sort on the second item of the tuples first then sort again on the first item of the tuple. – wwii May 16 '21 at 14:22

2 Answers2

7

Here is solution you can try out, using sorted + groupby

from itertools import groupby

# if data is already sorted, you can avoid this step.
all_combi = sorted(all_combi, key=lambda x: x[1])

print(
    [[i for i in v if i[0] in instr_list]  # filter out only required keys
     for _, v in groupby(all_combi, key=lambda x: x[1])]
)

[[('a', 33.333333333333336),
  ('b', 33.333333333333336),
  ('c', 33.333333333333336)],
 [('a', 38.333333333333336),
  ('b', 38.333333333333336),
  ('c', 38.333333333333336)],
...
sushanth
  • 8,275
  • 3
  • 17
  • 28
1

Try this:

def get_slab_list(all_combi, instr_list):
    all_combi.sort()
    op = []
    div = len(all_combi)//len(instr_list)
    for i in range(div):
        tmp = []
        for j in range(len(instr_list)):
            tmp.append(all_combi[j*div + i])
        op.append(tmp)

    return op


all_combi= [('a', 33.333333333333336), ('a', 38.333333333333336), ('a', 43.333333333333336), ('a', 48.333333333333336), ('a', 53.333333333333336), ('a', 58.333333333333336), ('a', 63.333333333333336), ('a', 68.33333333333334), ('a', 73.33333333333334), ('a', 78.33333333333334), ('a', 83.33333333333334), ('a', 88.33333333333334), ('a', 93.33333333333334), ('a', 98.33333333333334), ('b', 33.333333333333336), ('b', 38.333333333333336), ('b', 43.333333333333336), ('b', 48.333333333333336), ('b', 53.333333333333336), ('b', 58.333333333333336), ('b', 63.333333333333336), ('b', 68.33333333333334), ('b', 73.33333333333334), ('b', 78.33333333333334), ('b', 83.33333333333334), ('b', 88.33333333333334), ('b', 93.33333333333334), ('b', 98.33333333333334), ('c', 33.333333333333336), ('c', 38.333333333333336), ('c', 43.333333333333336), ('c', 48.333333333333336), ('c', 53.333333333333336), ('c', 58.333333333333336), ('c', 63.333333333333336), ('c', 68.33333333333334), ('c', 73.33333333333334), ('c', 78.33333333333334), ('c', 83.33333333333334), ('c', 88.33333333333334), ('c', 93.33333333333334), ('c', 98.33333333333334)]
instr_list=['a','b','c']
sample = get_slab_list(all_combi, instr_list)
print(sample)

output:

[[('a', 33.333333333333336), ('b', 33.333333333333336), ('c', 33.333333333333336)], [('a', 38.333333333333336), ('b', 38.333333333333336), ('c', 38.333333333333336)], [('a', 43.333333333333336), ('b', 43.333333333333336), ('c', 43.333333333333336)], [('a', 48.333333333333336), ('b', 48.333333333333336), ('c', 48.333333333333336)], [('a', 53.333333333333336), ('b', 53.333333333333336), ('c', 53.333333333333336)], [('a', 58.333333333333336), ('b', 58.333333333333336), ('c', 58.333333333333336)], [('a', 63.333333333333336), ('b', 63.333333333333336), ('c', 63.333333333333336)], [('a', 68.33333333333334), ('b', 68.33333333333334), ('c', 68.33333333333334)], [('a', 73.33333333333334), ('b', 73.33333333333334), ('c', 73.33333333333334)], [('a', 78.33333333333334), ('b', 78.33333333333334), ('c', 78.33333333333334)], [('a', 83.33333333333334), ('b', 83.33333333333334), ('c', 83.33333333333334)], [('a', 88.33333333333334), ('b', 88.33333333333334), ('c', 88.33333333333334)], [('a', 93.33333333333334), ('b', 93.33333333333334), ('c', 93.33333333333334)], [('a', 98`.33333333333334), ('b', 98.33333333333334), ('c', 98.33333333333334)]]
edusanketdk
  • 602
  • 1
  • 6
  • 11
  • If I want the b and c to be selected at random rather than the usual in the pre-defined what needs to be done, basically, I dont the same number(33.33 for example) for a,b,c in the tuple. – Sid May 16 '21 at 14:42
  • I have updated the question to showcase the other type of output that I am expecting. – Sid May 16 '21 at 15:50