0

Building up from this question, I have generated a selection of elements from a given list summing up to a user's input value. From the generated combination of numbers I want to change the status of pins on a raspberry pi on/off. i.e when maximum = 35 generate [1, 4, 10, 20] then choose 1:2, 4:17, 10:27, 20:22 then turn pins 2, 17, 27 & 22 on or if maximum = 30 output should give me [10, 20] & 10:27, 20:22 and turn pins 27 & 22 on

Using the dict I have

import itertools
maximum = 35

data = [1, 2, 3, 4, 10, 20]
pinList = [2, 3, 4, 17, 27, 22]
dictionary = dict(zip(data, pinList))

def selection(data, maximum):
    for count in range(1,len(data)+1):
        for combination in itertools.combinations(data, count):
            if maximum == sum(combination):
                return list(combination)

i = list(selection(data, maximum))
print (i)
k = dict((key, value) for key, value in dictionary.iteritems() if key == i)
print k
aux = range(len(k))
for r in aux:
   GPIO.setup(r, GPIO.OUT) 
   GPIO.output(r, GPIO.HIGH)

I am only getting

[1, 4, 10, 20] 
{}
Community
  • 1
  • 1
8host
  • 45
  • 8

1 Answers1

0

Change == to in:

k = dict((key, value) for key, value in dictionary.iteritems() if key in i)

Or more efficiently:

k = dict((key, dictionary[key]) for key in i)
Alex Hall
  • 34,833
  • 5
  • 57
  • 89