demo = [1,2,3,4,5]
转换成
[(1,2),(3,4),(5,None)]
demo = [1]
转换成
[(1,None)]
成双的组合,单个的加None,为空时什么都不干
4 回答
四季花海
TA贡献1811条经验 获得超5个赞
print map(None, *([iter([1,2,3,4,5])] * 2))
#[(1, 2), (3, 4), (5, None)]print map(None, *([iter([1])] * 2))
#[(1, None)]
月关宝盒
TA贡献1772条经验 获得超5个赞
list(dict(zip(demo[::2], demo[1::2] if len(demo)%2==0 else demo.append(None))).items())
还是分2行吧
demo if len(demo)%2 == 0 else demo.append(None)
list(dict(zip(demo[::2],demo[1::2])).items())
守候你守候我
TA贡献1802条经验 获得超10个赞
python3
>>> from itertools import zip_longest as zipl
>>> list(zipl(*([iter([1,2,3,4,5])] * 2)))
[(1, 2), (3, 4), (5, None)]
>>> list(zipl(*([iter([1])] * 2)))
[(1, None)]
添加回答
举报
0/150
提交
取消