从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]
{}
答案 0 :(得分:0)
将while(resultSet.next()){
更改为==
:
in
或更有效率:
k = dict((key, value) for key, value in dictionary.iteritems() if key in i)