2013-11-29 35 views

回答

4

您可以使用列表解析来得到这样的结果

data = [["1","2","3"],["7","6","5"]] 
print [[[int(j)] for j in i] for i in data ] 

输出

[[[1], [2], [3]], [[7], [6], [5]]] 
+0

它确实有帮助。谢谢 – SonicFancy

+0

@SonicFancy欢迎您:)请考虑接受此答案http://meta.stackexchange.com/a/5235/235416 – thefourtheye

1

替代@ thefourtheye的方法:

>>> import itertools 
>>> l = [["1","2","3"],["7","6","5"]] 
>>> result = [[int(i)] for i in itertools.chain.from_iterable(l)] 
>>> result 
[[1], [2], [3], [7], [6], [5]] 

其分解,这里是用正常的循环方式:

result = [] 
for i in itertools.chain.from_iterable(l): 
    result.append([int(i)]) 
1

递归函数不会使输入任何假设:

def list2list(l): 
    for i, e in enumerate(l): 
     if type(e) == list: list2list(e) 
     else: l[i] = [int(e)] 
    return l 

l = [["1","2","3"],["7","6","5"]] 
list2list(l) 
print l 

产品:

[[[1], [2], [3]], [[7], [6], [5]]] 
+0

他希望他们成为数字,我相信。 – thefourtheye

+0

另外,它仍然是分开:) – aIKid

+0

@aIKid,我认为这是所需的输出 – perreal