我有一个将数据从一种类型转换为另一种类型的脚本。 源文件可以有一个、两个或全部:位置、旋转和缩放数据。你知道吗
在对输出文件进行转换之后,我的脚本会将3个压缩到一起。你知道吗
在这种情况下,我的源文件只包含位置数据。 所以最后返回的列表是:
pData = [['-300.2', '600.5'],['150.12', '280.7'],['19.19', '286.56']]
rData = []
sData = []
translationData = list(zip(pData, rData, sData))
如果我尝试这样做,它将返回[]
,因为最短的列表是[]
。
如果我尝试:
translationData = list(zip_longest(pData, rData, sData))
我得到:
`[(['-300.2', '600.5'], None, None), (['150.12', '280.7'], None, None), (['19.19', '286.56'], None, None)]`
有没有办法只压缩包含数据的列表,或者从列表中的元组中删除None
?你知道吗
提前谢谢!你知道吗
您可以修改
zip_longest
的纯Python版本(给定documentation),并创建一个版本来执行您想要的操作:输出:
如果出于某种原因不想导入或使用列表理解等:
最后,*传递过滤分组的zip(*zippable)
只是另一个可能的解决方案
您可以使用嵌入在list comp中的^{} 内置项。你知道吗
结果:
相关问题 更多 >
编程相关推荐