1

I have an optimization problem of trying to use less variety of products.

For example:

screws = [6,8,16,18,39]

I would like to exchange those 5 screws with 3. So I need to use the strongest so I could select, [8,18,39]. Using any other option will cause a waste—for example, screw 16 is too strong for location of 6, so [16,18,39] is not as good. I would like to write an algorithm that would be useful for a larger number of parts also. So far I tried this:

def split_list(data, n):
    from itertools import combinations, chain
    for splits in combinations(range(1, len(data)), n-1):
        result = []
        prev = None
        for split in chain(splits, [None]):
            result.append(data[prev:split])
            prev = split
        yield result
new_list = list(split_list(screws, 3))       
#print list(split_list(screws, 3))

As a result of running this code I got a list of lists:

[[[6], [8], [16, 18, 39]], 
 [[6], [8, 16], [18, 39]], 
 [[6], [8, 16, 18], [39]], 
 [[6, 8], [16], [18, 39]], 
 [[6, 8], [16, 18], [39]], 
 [[6, 8, 16], [18], [39]]]

I would like to find out local maximums from all the list. For example in the first list [[6], [8],[16, 18, 39]], maximum = 6, maximum = 8, maximum = 39, and so on. But I don't know how to. Is there a way to find local maximum of all nested lists? I'm stuck in this moment, could you help me? I would appreciate also help with further progress.

Later on, I would like to check the sum of differences between maximum and other elements in the same list. So, 6-6 = 0, 8-8 = 0, and last 39-16+30-18-39-39 = 35. This will allow me to find out the smallest value from all lists of lists. This will be the most optimal solution. So the final result should be [[6, 8], [16, 18], [39]] and from it I would select [8,18,39].

This is basically my first program after tutorials and online classes, so all help is very welcome.

Kubix
  • 79
  • 1
  • 8
  • 2
    After editing this to be readable, I've now read it and… what's your actual question? You've got some code for the first step that works. You know what you want to write for the second step, but you haven't written it yet. You know what you want to do after that, but you haven't gotten there yet. So… everything sounds great. – abarnert Nov 14 '14 at 23:41
  • I'm stuck in the moment of finding local maximums. I don't know how to access values within nested lists. I'm also not sure how to write further part, but maybe after this step it will be smoother. Could you help me with finding local maximums in this nested loops? – Kubix Nov 14 '14 at 23:58
  • To access values in lists, you can iterate over them (`for value in lst:`) or index them (`lst[2]`), right? So, to access values in nested lists, you can iterate over (`for sublst in lst:`, `for value in sublst:`) or index (`lst[2][4]`) the inner lists the exact same way as the outer. – abarnert Nov 15 '14 at 00:04
  • I feel like my answer to your other question here http://stackoverflow.com/a/26936474/555632 gives you exactly what you want. Can you take another look at it? – arghbleargh Nov 15 '14 at 08:24

1 Answers1

0

You have a list of lists of lists so just iterate over the list then get the max of each of the sublists inside the sublists.

l = [[[6], [8], [16, 18, 39]],
 [[6], [8, 16], [18, 39]],
 [[6], [8, 16, 18], [39]],
 [[6, 8], [16], [18, 39]],
 [[6, 8], [16, 18], [39]],
 [[6, 8, 16], [18], [39]]]
for sub in l: # each sublist in l -> [[6], [8], [16, 18, 39]]etc..
    print([max(ele) for ele in sub]) # each sublist inside each sublist -> [6], [8], [16, 18, 39]

[6, 8, 39]
[6, 16, 39]
[6, 18, 39]
[8, 16, 39]
[8, 18, 39]
[16, 18, 39]

So in your code just do the following:

for sub in split_list(screws, 3):
    print([max(ele) for ele in sub])

To get the max minus each element you will have a lot of nested loops:

l = [[[6], [8], [16, 18, 39]],
 [[6], [8, 16], [18, 39]],
 [[6], [8, 16, 18], [39]],
 [[6, 8], [16], [18, 39]],
 [[6, 8], [16, 18], [39]],
 [[6, 8, 16], [18], [39]]]

result = []
for sub in l:
    for sub_ele in sub:
        mx = max(sub_ele)
        result.append([mx]+map(lambda x: mx-x,sub_ele))


[[6, 0], [8, 0], [39, 23, 21, 0], [6, 0], [16, 8, 0], [39, 21, 0], [6, 0], [18, 10, 2, 0], [39, 0], [8, 2, 0], [16, 0], [39, 21, 0], [8, 2, 0], [18, 2, 0], [39, 0], [16, 10, 8, 0], [18, 0], [39, 0]]
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • Thanks a lot, I would mark your answer as a good one , but as a new user I need to improve my reputation. Once I will get it I will mark your answer. – Kubix Nov 15 '14 at 17:04