如何通过将列表分成 python 中的更多列表来划分列表?
How do i section a list by dividing it into more lists in python?
好的,我在 python 中有一个列表,例如:
[['02357413'], [30], ['02357413'], [22], ['02357185'], [23], ['02357185'], [24]]
我想把它转换成这样:
[['02357413', 30], ['02357413', 22], ['02357185', 23], ['02357185', 24]]
提前致谢
您可以在 Python 中使用列表推导来执行以下操作:
>>> a = [['02357413'], [30], ['02357413'], [22], ['02357185'], [23], ['02357185'], [24]]
>>> [a[i]+a[i+1] for i in range(0,len(a)-1, 2)]
[['02357413', 30], ['02357413', 22], ['02357185', 23], ['02357185', 24]]
>>>
好的,我在 python 中有一个列表,例如:
[['02357413'], [30], ['02357413'], [22], ['02357185'], [23], ['02357185'], [24]]
我想把它转换成这样:
[['02357413', 30], ['02357413', 22], ['02357185', 23], ['02357185', 24]]
提前致谢
您可以在 Python 中使用列表推导来执行以下操作:
>>> a = [['02357413'], [30], ['02357413'], [22], ['02357185'], [23], ['02357185'], [24]]
>>> [a[i]+a[i+1] for i in range(0,len(a)-1, 2)]
[['02357413', 30], ['02357413', 22], ['02357185', 23], ['02357185', 24]]
>>>