将引脚列表映射到生成的组合列表

时间:2016-12-12 10:38:12

标签: python mapping itertools gpio

this question构建,我已经从给定列表中生成了一系列元素,总结为用户的输入值。从生成的数字组合我想要改变树莓派上的引脚状态开/关。即,当maximum = 35生成[1, 4, 10, 20]然后选择1:2, 4:17, 10:27, 20:22然后转pins 2, 17, 27 & 22 on或如果maximum = 30输出应该给我[10, 20]& 10:27, 20:22并转为pins 27 & 22 on

使用dict我有

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)

我只是

[1, 4, 10, 20] 
{}

1 个答案:

答案 0 :(得分:0)

while(resultSet.next()){ 更改为==

in

或更有效率:

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