将['0','0 1','0 1 2','0 1 2 3']重新格式化为['0',['0','1'],...]

时间:2018-12-13 06:20:24

标签: python

我有一个列表,因为UEID = ['0','0 1 ','0 1 2','0 1 2 3']需要输出为['0',['0','1'],...],即UEID[1][1]应该打印1

我的代码:

for x in UEID:
    if " " in UEID[x]:
        ueid_list = [int(j) for j in UEID[x]]
        print ueid_list

这显示为错误:

TypeError: list indices must be in integer, not str

1 个答案:

答案 0 :(得分:3)

尝试以下代码

UEID = ['0','0 1 ','0 1 2','0 1 2 3']
print [x.split() if len(x)>1 else x for x in UEID]