In [2]: [int(s.replace('[', '').replace(']', '')) for s in qtyList]
Out[2]: [40, 68, 18, 10]
另一种方式:
In [3]: import re
In [4]: [int(re.sub('[\[\]]', '', s)) for s in qtyList]
Out[4]: [40, 68, 18, 10]
这里有一个奇怪的方法,如果列表总是像你展示的那样交替出现:
In [5]: from itertools import cycle
In [6]: slices = cycle((slice(1, None), slice(None, -1)))
In [7]: [int(s[c]) for s, c in zip(qtyList, slices)]
Out[7]: [40, 68, 18, 10]
使用list comp和regexp是一种方法:
下面是一种遍历列表中每个列表项的方法。你知道吗
单向:
另一种方式:
这里有一个奇怪的方法,如果列表总是像你展示的那样交替出现:
相关问题 更多 >
编程相关推荐