我有一个列表,因为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
答案 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]