我已经看到许多使用串口的代码示例,人们说他们也在使用代码.问题是,当我尝试代码时,它不起作用.
import serial
ser = serial.Serial(
port=0,
baudrate=9600
# parity=serial.PARITY_ODD,
# stopbits=serial.STOPBITS_TWO,
# bytesize=serial.SEVENBITS
)
ser.open()
ser.isOpen()
print(ser.write(0xAA))
Run Code Online (Sandbox Code Playgroud)
它给我的错误是:"SerialException:Port已经打开".是我使用python3.3的问题还是我需要安装一些额外的东西?有没有其他方法可以使用Python端口与Python3.3?
首先,我能够做到,但我对速度不满意。
我的问题是,有没有更好、更快的方法来做到这一点?
我有一个如下所示的项目列表:
[(1,2), (1,2), (4,3), (7,8)]
Run Code Online (Sandbox Code Playgroud)
我需要获得所有独特的组合。例如,两个项目的独特组合将是:
[(1,2), (1,2)], [(1,2), (4,3)], [(1,2), (7,8)], [(4,3), (7,8)]
Run Code Online (Sandbox Code Playgroud)
使用 itertools.combinations 后,由于重复,我得到的比这多得多。例如,我得到包含 (1,2) 的每个列表两次。如果我创建一组这些组合,我会得到独特的组合。当原始列表有 80 个元组并且我想要其中包含 6 个项目的组合时,问题就出现了。完成该设置需要 30 秒以上。如果我能把这个数字降下来,我会很高兴。
我知道组合的数量很大,这就是为什么创建该组合非常耗时。但我仍然希望有一个库能够以某种方式优化该过程,从而加快速度。
值得注意的是,在我发现的所有组合中,我只测试了前 10000 个左右。因为在某些情况下,所有组合都可能需要处理太多,所以我不想在它们上花费太多时间,因为还有其他测试要做。
这是我现在拥有的示例:
from itertools import combinations
ls = [list of random NON-unique sets (x,y)]
# ls = [(1,2), (1,2), (4,3), (7,8)] # example
# in the second code snipped it is shown how I generate ls for testing
all_combos = combinations(ls, 6)
all_combos_set = set(all_combos)
for combo in all_combos_set:
do_some_test_on(combo) …Run Code Online (Sandbox Code Playgroud)