[英]Is there a more efficient way of combining data into a list?
For some background, I am using a Raspberry Pi 3 B+ and I am obtaining data from two ADXL345's.对于某些背景,我使用的是 Raspberry Pi 3 B+ 并且我正在从两个 ADXL345 获取数据。 No issue there.
没有问题。 Right now, the code is like this:
现在,代码是这样的:
x1, y1, z1 = accel1.read()
x2, y2, z2 = accel2.read()
coordinates.append([x1, y1, z1, x2, y2, z2, time.time()])
However, I want to figure out if it is possible to combine all of those statements into a single line along the lines of this, more or less:但是,我想弄清楚是否有可能将所有这些语句或多或少地合并为一行:
coordinates = [accel1.read(), accel2.read, time.time()]
One thing to note is that accel1
or accel2
output the data in a format like this: (0.0, 0.0, 0.0)
.需要注意的一件事是
accel1
或accel2
以如下格式输出数据: (0.0, 0.0, 0.0)
。 I have tried this but it gives an error.我试过这个,但它给出了一个错误。 Is there a way to streamline this process?
有没有办法简化这个过程?
Just unpack them using *
operator:只需使用
*
运算符解压它们:
def read():
return 1, 2, 3
lst = [*read()]
print(lst) # output [1, 2, 3]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.