将pin列表映射到生成的组合lis

2024-10-06 12:39:12 发布

您现在位置:Python中文网/ 问答频道 /正文

this question的基础上,我从一个给定的列表生成了一组元素,这些元素的总和为用户的输入值。从生成的数字组合中,我想更改raspberry pi on/off上管脚的状态。i、 e当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] 
{}

Tags: keyin元素fordatagpiodictionaryon